I’m trying to generate the following output:
type | totals |
---|---|
CostA | 1500 |
CostB | 200 |
CostC | 300 |
From a table with this structure:
CostA
CostB
CostC
name
date
-- more columns ...
The table has more columns but I want the sum for only 3 of them.
I need this structure specifically because superset expects a structure like this to generate some charts.
Any idea?
3
Answers
You can do it using
union all
:You can do it using
unnest()
as follows :If the source table is big, it pays to compute all sums in a single
SELECT
(close to 1/3 the cost), and then pivot results – with aVALUES
expression in aLATERAL
join:See:
Aside: avoid CaMeL-case identifiers in Postgres if at all possible. See: