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
 employee gender and department

Author  Topic 

codee240
Starting Member

1 Post

Posted - 2010-03-31 : 11:55:25
need help writing query which shows results like this ...

department male female
a 2 5
b 3 6
c 5 3
d 6 2
e 7 1
f 8 4
g 9 7

can anyone help please ...

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2010-03-31 : 12:00:52
sure..


select 'a' as department, 2 as male, 5 as female union all
select 'b',3,6 union all
select 'c',5,3 union all
select 'd',6,2 union all
select 'e',7,1 union all
select 'f',8,4 union all
select 'g',9,7


Easy enough?

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-01 : 05:25:44
Select
department ,
sum(case when sex='M' then 1 else 0 end) as male,
sum(case when sex='F' then 1 else 0 end) as female,
from your_table
group by department


Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-01 : 05:41:34
make sure you post your table structures along with some sample data while posting questions in future. they will enable us to give accurate and quick solns

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

Go to Top of Page
   

- Advertisement -