Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Group By + Count

Author  Topic 

kaushals
Starting Member

5 Posts

Posted - 2010-10-12 : 15:17:26
Hi,
I have two tables
Table 1: Project
Fields: projectid, projectname

Table 2: Employee
Fields: id, name, statusid, projectid

Statusid can be "New", "Assigned", "Completed"

I would like to get result in the format below.
Project Total New Assigned Completed
ABC0001 30 10 15 2
DEF002 40 20 20 0

I know how to get count using group by for projectname and toal but my question is how do I get the count for all different statusid for a particular project?

Thanks in advance for your help.

kaushals
Starting Member

5 Posts

Posted - 2010-10-12 : 15:31:53
I figured out the.
I need to write

select projectname, count(employee), count(case empstatus when 'New' then 1 end) as New, count(case empstatus when 'Assigned' then 1 end) and so on... and ofcourse i have to join the tables and group by....

Thanks to previous posts which helped me to figure out.

This forum rocks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-17 : 02:51:51
you can also use PIVOT without using group by if you're using sql 2005 or more

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-10-26 : 11:22:17
Here is the example for dynamic pivot
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -