Approach
Relational aggregation
For Reformat Department Table, the query transforms and combines relational rows, then filters or aggregates them into the requested result.
- Identify the source rows and join keys.
- Apply filters before aggregation when possible.
- Group, rank, or project the final columns required by the result.
Code notes
- 17 lines of SQL from the credited upstream file 1179.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Review join cardinality, grouping keys, and available indexes when estimating query cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1SELECT2 id,3 SUM(IF(month = 'Jan', revenue, NULL)) AS Jan_Revenue,4 SUM(IF(month = 'Feb', revenue, NULL)) AS Feb_Revenue,5 SUM(IF(month = 'Mar', revenue, NULL)) AS Mar_Revenue,6 SUM(IF(month = 'Apr', revenue, NULL)) AS Apr_Revenue,7 SUM(IF(month = 'May', revenue, NULL)) AS May_Revenue,8 SUM(IF(month = 'Jun', revenue, NULL)) AS Jun_Revenue,9 SUM(IF(month = 'Jul', revenue, NULL)) AS Jul_Revenue,10 SUM(IF(month = 'Aug', revenue, NULL)) AS Aug_Revenue,11 SUM(IF(month = 'Sep', revenue, NULL)) AS Sep_Revenue,12 SUM(IF(month = 'Oct', revenue, NULL)) AS Oct_Revenue,13 SUM(IF(month = 'Nov', revenue, NULL)) AS Nov_Revenue,14 SUM(IF(month = 'Dec', revenue, NULL)) AS Dec_Revenue15FROM Department16GROUP BY 1;17