Branch Sales Pivot
Start Timer
0:00:00
Your company, a multinational retail corporation, has been storing sales data from various branches worldwide in separate tables according to the year the sales were made. The current data structure is proving inefficient for business analytics and the management has requested your expertise to streamline the data.
Write a query to create a pivot table that shows total sales for each branch by year.
Note: Assume that the sales are represented by the total_sales column and are in USD. Each branch is represented by its unique branch_id.
Example:
Input:
For simplicity, consider two years: 2021 and 2022.
sales_2021 table
| Column | Type |
|---|---|
| id | INTEGER |
| branch_id | INTEGER |
| total_sales | INTEGER |
sales_2022 table
| Column | Type |
|---|---|
| id | INTEGER |
| branch_id | INTEGER |
| total_sales | INTEGER |
Output:
sales_pivot table
| Column | Type |
|---|---|
| branch_id | INTEGER |
| total_sales_2021 | INTEGER |
| total_sales_2022 | INTEGER |
This output pivot table shows the total sales for each branch, broken down by year.
.
.
.
.
.
.
.
.
.
Comments