954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sql count (distinct case

My table has these info

T123
T153
T174

Basic SQL count works fine which results to (3).. but when I use this function needed for my query the output is 4.

COUNT(DISTINCT CASE WHEN id LIKE '%T%' THEN RIGHT(ID, 3) ELSE 0 END) AS Count


what's wrong with my query,,, im stuck in hours :(

Please help

jcarbillon
Light Poster
43 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
 

If you have values that don't contain T then you are adding one to the count by returning to the count the number 0.
You can try to remove else entirely or replace 0 with NULL. I'm guessing both will work.

adam_k
Practically a Posting Shark
804 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

Can you post a more complete scenario (i.e. more sample data, a complete SQL statement, etc.) to work with? Just with what you've posted I did a little test and it worked fine for me.

BitBlt
Master Poster
711 posts since Feb 2011
Reputation Points: 367
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You