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 2000 Forums
 SQL Server Development (2000)
 group by

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2006-12-12 : 22:21:37
hi fyi,

table-gworker
idno unit sex
rab4012 30 m
rab4013 25 f
rab4014 30 m
rab4015 30 f

the result should be:
unit m f
30 2 1
25 0 1

select unit,count(sex)
from gworker group by sex.it works but it not shows like above output:

how to form a query using group by????????

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-12-12 : 22:41:32
[code]
select
unit,
M = sum(case when sex = 'm' then 1 else 0 end),
F = sum(case when sex = 'f' then 1 else 0 end)
from
gworker
group by
unit
order by
unit
[/code]

CODO ERGO SUM
Go to Top of Page
   

- Advertisement -