Zole72
Zole72

Reputation: 39

How to sum values if I don't have column to group it by

I need to sum sales grouped by country, but I have to group them manually because I don't have any other way. Unfortunately, I don't have the column 'continent', but there are not too many countries on the list so I can do it manually. I can't create any new columns in the table, so I need to do it in a query.

For example:

country | sum of sales
Germany    1000
Italy      500
Canada     700
UK         1300
USA        3000

I would like to see the total sales for Europe and North America

continent | sum of sales
Europe          2800
North America   3700

Upvotes: 1

Views: 73

Answers (1)

Guru Stron
Guru Stron

Reputation: 142038

You should be able to combine case expression and in predicate, something along this lines:

SELECT CASE
    WHEN country in ('Germany', 'UK') THEN 'Europe'
    WHEN country in ('Canada', 'USA') THEN 'North America'
    END as continent,
    sum("sum of sales")
FROM table
GROUP BY 1

Upvotes: 1

Related Questions