You are given a table Department that contains information about the revenue of each department per month. The table has the following structure:
id: Department identifier (int)revenue: Revenue amount for the department in a specific month (int)month: Month name as a string (varchar)
The month column contains values from ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
Task: Reformat the table to create a pivot table where:
- Each row represents a department (by id)
- There is a separate column for each month showing the revenue
- If a department has no revenue for a particular month, the value should be
NULL
Return the result table in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Department identifier |
revenue
|
int | Revenue amount for the department in the specified month |
month
PK
|
varchar | Month name (Jan, Feb, Mar, etc.) |
Input & Output
| id | revenue | month |
|---|---|---|
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | Apr_Revenue | May_Revenue | Jun_Revenue | Jul_Revenue | Aug_Revenue | Sep_Revenue | Oct_Revenue | Nov_Revenue | Dec_Revenue |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 8000 | 7000 | 6000 | |||||||||
| 2 | 9000 | |||||||||||
| 3 | 10000 |
The input table shows revenue data for three departments across different months. Department 1 has revenue in Jan (8000), Feb (7000), and Mar (6000). Department 2 only has Jan revenue (9000). Department 3 only has Feb revenue (10000). The pivot transformation creates one row per department with separate columns for each month, filling NULL where no data exists.
| id | revenue | month |
|---|---|---|
| 1 | 8000 | Jan |
| 1 | 7000 | Feb |
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | Apr_Revenue | May_Revenue | Jun_Revenue | Jul_Revenue | Aug_Revenue | Sep_Revenue | Oct_Revenue | Nov_Revenue | Dec_Revenue |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 8000 | 7000 |
This example shows a single department with revenue in only two months. The pivot creates one output row for department 1 with Jan_Revenue = 8000, Feb_Revenue = 7000, and NULL for all other months.
Constraints
-
1 ≤ id ≤ 1000 -
1 ≤ revenue ≤ 100000 -
monthis one of["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] -
Each
(id, month)combination appears at most once