Skip to content
Advertisement

SQL Group By, case when on aggregated

Can’t wrap my mind around the next task: I have a table, with some key, which represents some kind of group id. I would like to GROUP BY by this key and in resulted table show some columns from this table depending on the column value: If all the values in this group by this key in col1 are equal (same number or text), then show this exact value, if they are different (at least one of them) – show some kind like “Others”.

Example

Resulted table:

Postgres 9.4, if this matters.

Advertisement

Answer

You can use aggregation and case:

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