Table: Department
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | revenue | int | | month | varchar | +-------------+---------+ In SQL,(id, month) is the primary key of this table. The table has information about the revenue of each department per month. The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
Reformat the table such that there is a department id column and a revenue column for each month.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Department table: +------+---------+-------+ | id | revenue | month | +------+---------+-------+ | 1 | 8000 | Jan | | 2 | 9000 | Jan | | 3 | 10000 | Feb | | 1 | 7000 | Feb | | 1 | 6000 | Mar | +------+---------+-------+ Output: +------+-------------+-------------+-------------+-----+-------------+ | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue | +------+-------------+-------------+-------------+-----+-------------+ | 1 | 8000 | 7000 | 6000 | ... | null | | 2 | 9000 | null | null | ... | null | | 3 | null | 10000 | null | ... | null | +------+-------------+-------------+-------------+-----+-------------+ Explanation: The revenue from Apr to Dec is null. Note that the result table has 13 columns (1 for the department id + 12 for the months).
This problem requires reformatting a table to pivot the month
column into separate columns representing each month's revenue. The solution uses SQL's aggregate functions and conditional logic to achieve this.
The core idea is to use conditional aggregation. We group the data by id
(department ID) and then use SUM(CASE WHEN ... THEN ... END)
to calculate the revenue for each month. The CASE
statement checks the month
column and sums the revenue only when it matches the specific month. If a department doesn't have data for a particular month, SUM
will return NULL
for that month's revenue column.
SELECT
id,
SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue,
SUM(CASE WHEN month = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue,
SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue,
SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue,
SUM(CASE WHEN month = 'May' THEN revenue ELSE NULL END) AS May_Revenue,
SUM(CASE WHEN month = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue,
SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue,
SUM(CASE WHEN month = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue,
SUM(CASE WHEN month = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue,
SUM(CASE WHEN month = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue,
SUM(CASE WHEN month = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue,
SUM(CASE WHEN month = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
FROM Department
GROUP BY id;
This SQL query does the following:
SELECT id, ...
: Selects the department id
and creates columns for each month's revenue.SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue
: This is repeated for each month. It sums the revenue
only if the month
is 'Jan', otherwise it uses NULL
. The ELSE NULL
is crucial; without it, SUM
would treat non-matching rows as 0 and not handle missing data properly.FROM Department
: Specifies the table to query.GROUP BY id
: Groups the results by department id
, so we get one row per department.Time Complexity: The time complexity is dominated by the GROUP BY
operation. In the worst case, this involves sorting or hashing the input rows, which typically takes O(N log N) or O(N) time, where N is the number of rows in the Department
table. The CASE
statements within the SUM
function adds a constant factor to this time complexity but doesn't alter the overall order.
Space Complexity: The space complexity is determined by the size of the output table, which depends on the number of distinct departments and the number of months. In the worst case, if each department has data for every month, the space complexity is O(M * D), where M is the number of months (12 in this case) and D is the number of distinct departments. The intermediate space used during the GROUP BY
operation also contributes to the space complexity, but is likely smaller than the output size.
This solution efficiently pivots the table using standard SQL features. The alternative approaches would involve more complex joins or subqueries and would likely be less efficient.