Skip to content
Quantoros

You are given this wide CTE. Unpivot it into rows (product, quarter, revenue): WITH wide AS (SELECT "AlphaPad" AS…

SQLLevel: IntermediateConditional aggregation and pivots

You are given this wide CTE. Unpivot it into rows (product, quarter, revenue): WITH wide AS (SELECT "AlphaPad" AS product, 100 AS q1, 200 AS q2, 150 AS q3, 300 AS q4)

Table definitions (from the source exercise):

Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: One SELECT per quarter with a literal quarter label, glued by UNION ALL.

WITH wide AS (SELECT 'AlphaPad' AS product, 100 AS q1, 200 AS q2, 150 AS q3, 300 AS q4)
SELECT product, 'Q1' AS quarter, q1 AS revenue FROM wide
UNION ALL SELECT product, 'Q2', q2 FROM wide
UNION ALL SELECT product, 'Q3', q3 FROM wide
UNION ALL SELECT product, 'Q4', q4 FROM wide;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions