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

COUNT() Function

Hi Everyone,
Thanks for your time.

I have a question about COUNT in sql. Does anyone know how to display the COUNT if it is equal to 0?

Please feel free to ask me any question if you don't understand my enquire.

Thanks for the post.

strRusty_gal
Light Poster
35 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
select last_name, count(*) total from user_master 
group by last_name
having count(*) = 1


This query will find last_name that occurs only once in the table.
so you must usehaving with aggregate functions.

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

The difficulty is that count() works against summarising existing rows in a table. By definition a count of 0 means that no rows exist so nothing will appear. How would SQL know what the missing values were. To address the problem you will need to set up a reference table with all the valid values in it and do an outer join against the table with the data in it.

ChrisPadgham
Posting Pro in Training
413 posts since Sep 2009
Reputation Points: 102
Solved Threads: 78
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You