▸_sqlgym

80. Two-Dimensional Pivot: Store by Quarter

HardPivotingconditional aggregationtwo-dimensional pivotSUMCASE

A two-dimensional pivot groups by one dimension on the rows and spreads a second dimension across the columns. Here each row is a store and each column is a quarter, with summed units in the cells.

Task: the sales2d table has one row per store per quarter per product (multiple products per store-quarter). Pivot to one row per store with the total units sold in each quarter as columns q1..q4. Return store, q1, q2, q3, q4. Use SUM(CASE WHEN quarter='Qx' THEN units END) so multiple product rows in the same store-quarter are added together. Order by store.

Output columns, in order: store, q1, q2, q3, q4.

Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…

This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases 5test cases in all. A sloppy query that only fits the example won't pass.

SQL
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.