You're a data analyst working with a company's monthly revenue data. The finance team has provided you with a database table that stores each department's revenue for different months in a normalized format (each row represents one department-month combination).
Your task is to pivot this data into a more readable format where each department appears as a single row with separate columns for each month's revenue.
Input Format:
A table named Department with the following structure:
| Column Name | Type |
|---|---|
| id | int |
| revenue | int |
| month | varchar |
Where (id, month) is the primary key, and month contains values from ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
Output Format:
Transform the table so that there's one row per department with columns for id and each month's revenue (Jan_Revenue, Feb_Revenue, etc.).
Input & Output
Visualization
Time & Space Complexity
Single pass through the table with GROUP BY aggregation
Space for d departments in the result set
Constraints
- 1 โค Department table rows โค 1000
- 1 โค id โค 100
- 0 โค revenue โค 106
- month is one of ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
- (id, month) forms the primary key - no duplicate combinations