Yilmazam
Yilmazam

Reputation: 59

multiple group by for a table query

I have a table like this:

    Month     Type        Price    
    ============================
    1          a           12     
    2          b           43
    1          a           11
    4          c           22
    1          b           33
    2          c           4
    3          a           25
    2          b           35
    4          c           20

I want to get a query that has result some thing like this:

    Month     Type     Total Price    
    ============================
    1          a           23     
    1          b           33
    2          b           78
    2          c           4
    3          a           25
    4          c           44

means: prices are Total Price of special Type in a Month.

for example we have type 'a' in month '1' and '3' Total Prices of 'a' in month '1' is 23 and in month '3' is 25

I think we should use multiple group by. I can group it just by Type or Month but not by both of them.

thanks for helping

Upvotes: 0

Views: 53

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112712

You can specify a list of expressions in the GROUP BY clause

SELECT  Month, Type, SUM(Price) AS [Total Price]
FROM MyTable
GROUP BY Month, Type
ORDER BY Month, Type

In GROUP BY, list all the involved columns, except those that have an aggregate function (SUM, MIN, MAX, AVG etc.) applied to them.

Upvotes: 4

Related Questions