我有一个由count函数生成的表。
No of Employee Employment Status Department Status
3 DIRECT ACCTG REGISTERED
1 COOP HR UNREGISTERED
2 DIRECT ACCTG REGISTERED
1 DIRECT ACCTG UNREGISTERED
5 DIRECT IT REGISTERED
3 COOP ACCTG REGISTERED
2 COOP MARKETING UNREGISTERED
如何创建这样的报告?
Department DIRECT COOP REGISTERED UNREGISTERED Total
ACCTG 6 3 8 1 9
答案 0 :(得分:0)
CASE
的聚合
SELECT Department,
SUM( Case when [Employment status] ='DIRECT' then [No of Employee] end) as DIRECT,
SUM( Case when [Employment status] ='COOP' then [No of Employee] end) as COOP,
SUM( Case when [Employment status] ='REGISTERED' then [No of Employee] end) as REGISTERED,
SUM( Case when [Employment status] ='UNREGISTERED' then [No of Employee] end) as UNREGISTERED,
SUM( Case when [Employment status] ='REGISTERED' then [No of Employee] end) as REGISTERED,
SUM( Case when [Employment status] in ( 'DIRECT', 'COOP') then [No of Employee] end) as Total
GROUP BY Department