79. Pivot Months then Add a Total
HardPivotingconditional aggregationSUMderived column
After pivoting you can compute a derived column from the pivoted values. Here you pivot revenue into per-month columns and then add a total that sums them.
Task: the revenue table has one row per region per month ('Jan', 'Feb', 'Mar'). Pivot it to one row per region with columns jan, feb, mar, then add total = jan + feb + mar. Every region has all three months, so no NULL handling is needed. Return region, jan, feb, mar, total. Order by region.
Output columns, in order: region, jan, feb, mar, total.
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· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.