Skip to content
Advertisement

Aggregation with required columns in GROUP BY clause in postgresSQL

DB-Fiddle

Expected Result:


To get the expected result I tried to go wiht this query:

However, it gives me error: column s.sales_channe" must appear in the GROUP BY clause.
The problem is when I add the sales_channel to the GROUP BY clause I am not able to ge the expected result because then it groups both by campaign and sales_channel.

How do I have to modify the query to get the expected result as in MariaDB?

Advertisement

Answer

You have to aggregate sales_channel and product in some way. For sales_channel, it doesn’t really matter how you aggregate it, because there’s only one option per campaign. Product, on the other hand, has multiple options. It appears that you want the maximum product, so choose that:

Fiddle

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement