MS Access组特定值一起使用

时间:2015-03-10 21:53:50

标签: sql ms-access

所以我正在解决MS Access中的一个问题,我需要一些按特定值分组的聚合值。

ExTable1:

  Type    TotalHours
+-------+------------+
|  A          10     |
|  A          20     |
|  A          30     |       
|  B          10     |
|  C          10     |
|  D          10     |
|  E          10     |
|  F          10     |
+-------+------------+

我有这个问题:

SELECT Type, SUM(TotalHours)
FROM ExTable1
GROUP BY Type

它将所有的A,B,C等组合在一起,但我也想在同一个查询中将E和F的值组合在一起。我该怎么做?

2 个答案:

答案 0 :(得分:2)

考虑使用Union Query:

SELECT Type, Sum(TotalHours) As TotalHours
FROM ExTable1
WHERE Type IN ('A', 'B', 'C', 'D')
GROUP BY Type;

UNION SELECT 'E & F', Sum(TotalHours)
FROM ExTable1
WHERE Type IN ('E', 'F');

答案 1 :(得分:1)

使用Conditional Aggregate

SELECT Type, 
       SUM(TotalHours),
       SUM(case when Type in ('E','F') then TotalHours else 0 END) as 'E&F total'
FROM ExTable1
GROUP BY Type