Problems / Unpivot Then Aggregate / Editorial
'visits' AS metric
visits AS value
UNION ALL
GROUP BY metric
SUM(value)
DuckDB also supports this directly with UNPIVOT daily_metrics ON visits, signups, purchases INTO NAME metric VALUE value, but UNION ALL is portable to any dialect. In PySpark, the same idea is one select per column with F.lit for the metric name, chained with .unionAll, then groupBy('metric').agg(F.sum('value')).
UNPIVOT daily_metrics ON visits, signups, purchases INTO NAME metric VALUE value
select
F.lit
.unionAll
groupBy('metric').agg(F.sum('value'))
Aggregating across *columns* is awkward — SUM works down rows, not across a row. After reshaping wide to long, "total per metric" becomes the most ordinary GROUP BY imaginable. Reshape first and the aggregation writes itself; the same trick powers most reporting pipelines: keep (or reshape) data long, aggregate cheap.
Solve Unpivot Then Aggregate yourself →