1

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
jarlh
  • 42,561
  • 8
  • 45
  • 63
Zole72
  • 39
  • 4

1 Answers1

1

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
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • I'm writing my query in PrestoDB and getting the following message: "java.sql.SQLException: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 2:26: IN value and list items must be the same type: array(varchar) [Execution ID: 55b867bb-8e4e-483b-a91f-215dcbeb464c]" – Zole72 Oct 11 '21 at 11:47
  • 1
    Just a small note: `case` _expression_, `in` _predicate_. – jarlh Oct 11 '21 at 12:03
  • @Zole72 was not able to reproduce, [this](https://pastebin.com/fDKTVd8S) works just fine for me. – Guru Stron Oct 11 '21 at 12:23