Monthly Revenue Pivot by Region

Quick Overview

Write a SQL query that turns individual sales rows into a monthly report with one revenue column per region plus a total column, showing zero for any region with no sales that month. It tests conditional aggregation, date bucketing by year and month, and NULL handling when pivoting rows into columns.

Monthly Revenue Pivot by Region

Company: Cloudflare

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Produce a monthly revenue report from `sales`, pivoted by region, with one row per month. Each row gives the month as a `YYYY-MM` string, the total revenue for each of the four regions in its own column, and a `Total` column with the revenue across all regions. A region with no sales in a month shows `0.00`. Order the rows by month ascending. ### Input Table(s) `sales` | Column | Type | Meaning | | --- | --- | --- | | `id` | INT | Primary key, one row per sale | | `region` | VARCHAR(20) | Region of the sale, never NULL, one of `'North'`, `'South'`, `'East'`, `'West'` | | `amount` | DECIMAL(10,2) | Sale amount, never NULL, always positive | | `sale_date` | DATE | Date of the sale, never NULL | Primary key: `id`. A region can have many sales in the same month. ### Output Contract | Column | Type | Meaning | | --- | --- | --- | | `sale_month` | TEXT | Year and month in `YYYY-MM` format | | `North` | DECIMAL | Sum of `amount` for North sales that month, `0.00` if none | | `South` | DECIMAL | Sum of `amount` for South sales that month, `0.00` if none | | `East` | DECIMAL | Sum of `amount` for East sales that month, `0.00` if none | | `West` | DECIMAL | Sum of `amount` for West sales that month, `0.00` if none | | `Total` | DECIMAL | Sum of `amount` over all sales that month | Column names are case-sensitive and must match exactly (`North`, not `north`). All money columns keep two decimal places. Return one row per month that has at least one sale, ordered by `sale_month` ascending; `sale_month` is unique in the output, so the order is fully determined. ### Example Input `sales`: | id | region | amount | sale_date | | --- | --- | --- | --- | | 1 | North | 100.00 | 2024-01-03 | | 2 | South | 50.50 | 2024-01-15 | | 3 | North | 25.00 | 2024-01-20 | | 4 | East | 80.00 | 2024-02-02 | | 5 | West | 40.25 | 2024-02-14 | | 6 | East | 19.75 | 2024-02-28 | | 7 | West | 60.00 | 2023-12-09 | Expected output: | sale_month | North | South | East | West | Total | | --- | --- | --- | --- | --- | --- | | 2023-12 | 0.00 | 0.00 | 0.00 | 60.00 | 60.00 | | 2024-01 | 125.00 | 50.50 | 0.00 | 0.00 | 175.50 | | 2024-02 | 0.00 | 0.00 | 99.75 | 40.25 | 140.00 | January's North column adds sales 1 and 3 (100.00 + 25.00 = 125.00), and East and West had no January sales, so they show 0.00. December 2023 sorts first even though its sale has the highest `id`. ### Constraints and Clarifications - The table holds at most 10,000 rows. - `region` is always exactly one of `'North'`, `'South'`, `'East'`, `'West'`, with that capitalization. No column contains NULL. - `amount` is positive and `sale_date` is a valid date. - A month with no sales in any region does not appear in the output. - The same month in different years forms separate rows. - A missing region in a month must appear as `0.00`, not NULL. ```hint One group, several sums Every output column is computed over the same month's rows. Think about how a single aggregate can be limited to the rows of one region. ```

Overview: Write a SQL query that turns individual sales rows into a monthly report with one revenue column per region plus a total column, showing zero for any region with no sales that month. It tests conditional aggregation, date bucketing by year and month, and NULL handling when pivoting rows into columns.

Cloudflare logo
Cloudflare
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Produce a monthly revenue report from sales, pivoted by region, with one row per month. Each row gives the month as a YYYY-MM string, the total revenue for each of the four regions in its own column, and a Total column with the revenue across all regions. A region with no sales in a month shows 0.00. Order the rows by month ascending.

Input Table(s)

sales

ColumnTypeMeaning
idINTPrimary key, one row per sale
regionVARCHAR(20)Region of the sale, never NULL, one of 'North', 'South', 'East', 'West'
amountDECIMAL(10,2)Sale amount, never NULL, always positive
sale_dateDATEDate of the sale, never NULL

Primary key: id. A region can have many sales in the same month.

Output Contract

ColumnTypeMeaning
sale_monthTEXTYear and month in YYYY-MM format
NorthDECIMALSum of amount for North sales that month, 0.00 if none
SouthDECIMALSum of amount for South sales that month, 0.00 if none
EastDECIMALSum of amount for East sales that month, 0.00 if none
WestDECIMALSum of amount for West sales that month, 0.00 if none
TotalDECIMALSum of amount over all sales that month

Column names are case-sensitive and must match exactly (North, not north). All money columns keep two decimal places. Return one row per month that has at least one sale, ordered by sale_month ascending; sale_month is unique in the output, so the order is fully determined.

Example

Input sales:

idregionamountsale_date
1North100.002024-01-03
2South50.502024-01-15
3North25.002024-01-20
4East80.002024-02-02
5West40.252024-02-14
6East19.752024-02-28
7West60.002023-12-09

Expected output:

sale_monthNorthSouthEastWestTotal
2023-120.000.000.0060.0060.00
2024-01125.0050.500.000.00175.50
2024-020.000.0099.7540.25140.00

January's North column adds sales 1 and 3 (100.00 + 25.00 = 125.00), and East and West had no January sales, so they show 0.00. December 2023 sorts first even though its sale has the highest id.

Constraints and Clarifications

  • The table holds at most 10,000 rows.
  • region is always exactly one of 'North' , 'South' , 'East' , 'West' , with that capitalization. No column contains NULL.
  • amount is positive and sale_date is a valid date.
  • A month with no sales in any region does not appear in the output.
  • The same month in different years forms separate rows.
  • A missing region in a month must appear as 0.00 , not NULL.