Advanced
Open
Pro
Pivoting Monthly Revenue by Category Into Columns
Given a normalized fact table:
monthly_category_revenue
month_num | category | revenue
----------+-------------+--------
1 | Electronics | 50000
1 | Apparel | 30000
2 | Electronics | 62000
2 | Apparel | 28000
2 | Home | 15000
3 | Electronics | 58000
3 | Apparel | 33000
Produce a report with one row per month and one column per
category (electronics_revenue, apparel_revenue,
home_revenue, ...), so it can be dropped straight into a
spreadsheet-style view.
- Write the pivot assuming the category list is known and fixed ahead of time.
- Explain, in words (no need for full working SQL), what breaks about this approach the moment a new category can appear in the data at any time, and what you'd actually do about it in a production pipeline rather than at query time.
Share this question