We have a Postgres table that looks like below. How to create a three column one row view that shows summary of the table:
- Maximum of all values
- Maximum of all values with category 1
- Maximum of all values with category 2
| category | value |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
| 2 | 4 |
>Solution :
You could use conditional aggregation…
select
max(value) as max_all,
max(case when category = 1 then value end) as max_cat1,
max(case when category = 2 then value end) as max_cat2
From
table1
| max_all | max_cat1 | max_cat2 |
|---|---|---|
| 4 | 3 | 4 |