Dataford
Interview Guides
Upgrade
All questions/SQL & Data Manipulation/Pivot Monthly Sales by Category for Dashboard Reporting

Pivot Monthly Sales by Category for Dashboard Reporting

Medium
SQL & Data Manipulation
Asked at 48 companies48Data Wrangling
Also asked at
EnteloGuildMECLABSQantasToyota MotorShape Security

Problem

Scenario

TechMart tracks all sales in a single sales table. The analytics team needs a dashboard that shows, for each month, the total sales amount for each product category as separate columns.

Task

Write a SQL query to pivot the sales data so that each row represents a month (in YYYY-MM format), and each column shows the total sales for a specific category. Only include sales from January to March 2024 (inclusive).

Requirements

  1. Output one row per month in the range, with months formatted as YYYY-MM.
  2. Columns for total sales of 'Electronics', 'Clothing', and 'Books'.
  3. If a category has no sales in a month, show NULL for that column.
  4. Order results by month ascending.

Schema

sales

ColumnTypeDescription
idINTPrimary key for the sales record
categoryVARCHAR(255)Product category for the sale
sale_dateDATEDate of the sale
amountDECIMAL(10,2)Amount of the sale

Sample Data

idcategorysale_dateamount
1Electronics2024-01-15200
2Clothing2024-01-20150
3Electronics2024-02-10300
4Books2024-02-15100
5Clothing2024-02-25250
6Electronics2024-03-05400
7Books2024-03-10200
8Clothing2024-03-15300
9Electronics2023-12-31500
10Books2024-01-10NULL
11Clothing2024-02-010
12Electronics2024-03-310

Expected Output

monthElectronicsClothingBooks
2024-01200150NULL
2024-02300250100
2024-03400300200

Schema

sales
ColumnTypeDescription
idPKINTPrimary key for the sales record
categoryVARCHAR(255)Product category for the sale
sale_dateDATEDate of the sale
amountDECIMAL(10,2)Amount of the sale

Sample Data

Example 1
Input{"sales":[[1,"Electronics","2024-01-15",200],[2,"Clothing","2024-01-20",150],[3,"Electronics","2024-02-10",300],[4,"Books","2024-02-15",100],[5,"Clothing","2024-02-25",250],[6,"Electronics","2024-03-05",400],[7,"Books","2024-03-10",200],[8,"Clothing","2024-03-15",300],[9,"Electronics","2023-12-31",500],[10,"Books","2024-01-10",null],[11,"Clothing","2024-02-01",0],[12,"Electronics","2024-03-31",0]]}Output[["2024-01",200,150,null],["2024-02",300,250,100],["2024-03",400,300,200]]

Problem

Scenario

TechMart tracks all sales in a single sales table. The analytics team needs a dashboard that shows, for each month, the total sales amount for each product category as separate columns.

Task

Write a SQL query to pivot the sales data so that each row represents a month (in YYYY-MM format), and each column shows the total sales for a specific category. Only include sales from January to March 2024 (inclusive).

Requirements

  1. Output one row per month in the range, with months formatted as YYYY-MM.
  2. Columns for total sales of 'Electronics', 'Clothing', and 'Books'.
  3. If a category has no sales in a month, show NULL for that column.
  4. Order results by month ascending.

Schema

sales

ColumnTypeDescription
idINTPrimary key for the sales record
categoryVARCHAR(255)Product category for the sale
sale_dateDATEDate of the sale
amountDECIMAL(10,2)Amount of the sale

Sample Data

idcategorysale_dateamount
1Electronics2024-01-15200
2Clothing2024-01-20150
3Electronics2024-02-10300
4Books2024-02-15100
5Clothing2024-02-25250
6Electronics2024-03-05400
7Books2024-03-10200
8Clothing2024-03-15300
9Electronics2023-12-31500
10Books2024-01-10NULL
11Clothing2024-02-010
12Electronics2024-03-310

Expected Output

monthElectronicsClothingBooks
2024-01200150NULL
2024-02300250100
2024-03400300200

Schema

sales
ColumnTypeDescription
idPKINTPrimary key for the sales record
categoryVARCHAR(255)Product category for the sale
sale_dateDATEDate of the sale
amountDECIMAL(10,2)Amount of the sale

Sample Data

Example 1
Input{"sales":[[1,"Electronics","2024-01-15",200],[2,"Clothing","2024-01-20",150],[3,"Electronics","2024-02-10",300],[4,"Books","2024-02-15",100],[5,"Clothing","2024-02-25",250],[6,"Electronics","2024-03-05",400],[7,"Books","2024-03-10",200],[8,"Clothing","2024-03-15",300],[9,"Electronics","2023-12-31",500],[10,"Books","2024-01-10",null],[11,"Clothing","2024-02-01",0],[12,"Electronics","2024-03-31",0]]}Output[["2024-01",200,150,null],["2024-02",300,250,100],["2024-03",400,300,200]]
Practice SQL
PostgreSQL
Open on desktop for the full SQL editor with syntax highlighting and autocomplete.
Up next
WalgreensMonthly Sales Aggregation by Product CategoryMediumHD SupplyMonthly Sales Trends by CategoryMediumIlluminaCalculate Monthly Sales Growth by Product CategoryMedium
Next question