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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Pivot table

Author  Topic 

winchmore
Starting Member

1 Post

Posted - 2014-10-16 : 19:03:15
Hi i got a group by query with the result:

Select Department ,gender , count(EmpID) as totalcount from employee
group by Department ,gender


Department gender totalcount
A M 5
A F 5
B M 2
B F 3
C F 10

what i would like is :

Department M F
A 5 5
B 2 3
C 10

Any help will be great :)








nagino
Yak Posting Veteran

75 Posts

Posted - 2014-10-16 : 20:50:34
like following?
SELECT Department, M, F FROM SOURCE
PIVOT
(
COUNT(EmpID)
FOR gender IN (M, F)
) AS PVT


-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

em172967
Starting Member

10 Posts

Posted - 2014-10-17 : 11:04:25
I would do Select Department, Case when gender = 'M' then count(empid) else 0 end as M_Count, Case when gender = 'F' then count(empid) else 0 end as F_count
from my table

quote:
Originally posted by winchmore

Hi i got a group by query with the result:

Select Department ,gender , count(EmpID) as totalcount from employee
group by Department ,gender


Department gender totalcount
A M 5
A F 5
B M 2
B F 3
C F 10

what i would like is :

Department M F
A 5 5
B 2 3
C 10

Any help will be great :)










Go to Top of Page
   

- Advertisement -