Chase W.
Chase W.

Reputation: 1413

MS Access Group Specific Values Together

So I am working on a problem in MS Access where I need some aggregated values grouped by certain values.

ExTable1:

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

And I have this query:

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

This groups all of the A's, B's, C's, etc.. together, but I also want to group E and F's values together in the same query. How would I do this?

Upvotes: 0

Views: 86

Answers (2)

Parfait
Parfait

Reputation: 107652

Consider using a 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');

Upvotes: 2

Pரதீப்
Pரதீப்

Reputation: 93724

Use 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

Upvotes: 1

Related Questions