runtime
76 / 88

lesson 76 Warehouse & reporting projects · advanced

Monthly change & cumulative reports

Measure monthly change and cumulative totals.

the idea

Monthly reports aggregate to one row per month first, then use window functions across months: SUM(…) OVER (ORDER BY month) for cumulative sales and LAG(…) for the previous month’s value and the percentage change.

Dataset: Retail

fact_sales7 columns · 45,151 rows
sale_keynumber · unique idorder_idnumbercustomer_keynumber · links to dim_customerproduct_keynumber · links to dim_productorder_datedatequantitynumbersalesdecimal
65625367955620622011-01-04834
6562636795568812011-01-0417.95
6562736795568872011-01-0414.95
6562836795564012011-01-0425.9
65629367955640592011-01-043630.6
65630367955643012011-01-041029.5
65631367955611502011-01-04617.7
65632367955620512011-01-0465.1
6563336795565102011-01-0486.8
6563436795567112011-01-042415.6
sale_keynumber · unique id
65625
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
2062
order_datedate
2011-01-04
quantitynumber
8
salesdecimal
34
sale_keynumber · unique id
65626
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
881
order_datedate
2011-01-04
quantitynumber
1
salesdecimal
7.95
sale_keynumber · unique id
65627
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
887
order_datedate
2011-01-04
quantitynumber
1
salesdecimal
4.95
sale_keynumber · unique id
65628
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
401
order_datedate
2011-01-04
quantitynumber
2
salesdecimal
5.9
sale_keynumber · unique id
65629
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
4059
order_datedate
2011-01-04
quantitynumber
36
salesdecimal
30.6
sale_keynumber · unique id
65630
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
4301
order_datedate
2011-01-04
quantitynumber
10
salesdecimal
29.5
sale_keynumber · unique id
65631
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
1150
order_datedate
2011-01-04
quantitynumber
6
salesdecimal
17.7
sale_keynumber · unique id
65632
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
2051
order_datedate
2011-01-04
quantitynumber
6
salesdecimal
5.1
sale_keynumber · unique id
65633
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
510
order_datedate
2011-01-04
quantitynumber
8
salesdecimal
6.8
sale_keynumber · unique id
65634
order_idnumber
3679
customer_keynumber · links to dim_customer
556
product_keynumber · links to dim_product
711
order_datedate
2011-01-04
quantitynumber
24
salesdecimal
15.6

first 10 of 45,151 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. Aggregate sales by month first. One row per month with rounded totals—that monthly grain is what running sums and month-over-month windows need.the pattern

    example.sql
    SELECT substr(order_date, 1, 7) AS month, ROUND(SUM(sales), 2) AS sales
    FROM fact_sales
    GROUP BY month
    ORDER BY month;
  2. Find the busiest months by orders. Distinct order_id per month shows which months drove the most orders—November usually leads for this wholesaler.

    step-2.sql
    SELECT
      substr(order_date, 1, 7) AS month,
      COUNT(DISTINCT order_id) AS orders
    FROM fact_sales
    GROUP BY month
    ORDER BY orders DESC
    LIMIT 3;
  3. Compare each month to the prior one. After aggregating to months, LAG(orders) pulls the previous month’s value on the same result set.

    step-3.sql
    WITH m AS (
      SELECT
        substr(order_date, 1, 7) AS month,
        COUNT(DISTINCT order_id) AS orders
      FROM fact_sales
      GROUP BY month
    )
    SELECT
      month,
      orders,
      LAG(orders) OVER (ORDER BY month) AS previous_orders
    FROM m
    ORDER BY month;

common mistake

Applying LAG before aggregating compares sales lines, not months.

challenge 1

Find each month, ordered by month.

Return month (YYYY-MM), sales (rounded to 2 decimals), and running_sales (cumulative sales through that month, rounded to 2 decimals).

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Monthly change & cumulative reports — Warehouse & reporting projects · Runtime