如何使用聚合函数生成访问报告?

时间:2014-11-11 01:23:29

标签: ms-access

我有一个由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

1 个答案:

答案 0 :(得分:0)

可以使用基于{p> 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