Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Sep 18, 1998

using COUNT and GROUP BY with zero-values

Question:
I'm trying to query our Human Resources database to find the number of temporary employees in each department. I'm using the following SQL statement:
select count (*)
from employee
where status = 'Temporary'
group by departments;
I expect the following results:
3
0
0
4
2
0
2
but am getting:
3
4
2
2
That is, the zero-count departments are not in my results. Is there a way to specify that I want the zeros returned, too?

Answer:
Using SQL Server you need add only one more word to obtain the desired result. Change the group by departments to group by ALL departments. The "all" keyword tells SQL Server to return all departments, even those with a count of 0.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date