#816

Unfold an Excel-style table back into a list

Reverse problem: the data arrived in «wide» form — one row per product, with three months' sums spread across three separate columns m1, m2, m3. Analytics prefers a «long» layout where every «product + month» combination is its own row. Postgres has no native «expand columns into rows» operation, but you can build it by combining several queries. Build a wide_sales table with columns product (string up to 50 chars), m1, m2, m3 (numbers), insert three rows for products A, B, and C. The output should produce three rows per product: «product, month number from 1 to 3, sum for that month». Sort by product, then by month number.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables.

monthamountproduct
1100A
2150A
3120A

Your query result will appear here