runtime
75 / 88

lesson 75 Warehouse & reporting projects · advanced

Customer & product reporting contracts

Produce customer and product reports at a defined level of detail.

the idea

A reporting contract fixes the grain, columns and rules of a report so every reader gets the same numbers: one row per customer (including customers with no sales), a defined reference date for recency, and explicit thresholds for segments.

Dataset: Retail

dim_customer5 columns · 848 rows
customer_keynumber · unique idcustomer_idtextcontact_nametextcountrytextregiontext
112347Arthur TurnerIcelandEurope
212353Harry TurnerBahrainRest of World
312359Theo TurnerCyprusEurope
412373Harry HillAustriaEurope
512374Emily HillAustriaEurope
612384Ava MooreSwitzerlandEurope
712389Leo MooreAustraliaRest of World
812392Lily MooreAustraliaRest of World
912404Ava SmithFinlandEurope
1012416Sophie SmithAustraliaRest of World
customer_keynumber · unique id
1
customer_idtext
12347
contact_nametext
Arthur Turner
countrytext
Iceland
regiontext
Europe
customer_keynumber · unique id
2
customer_idtext
12353
contact_nametext
Harry Turner
countrytext
Bahrain
regiontext
Rest of World
customer_keynumber · unique id
3
customer_idtext
12359
contact_nametext
Theo Turner
countrytext
Cyprus
regiontext
Europe
customer_keynumber · unique id
4
customer_idtext
12373
contact_nametext
Harry Hill
countrytext
Austria
regiontext
Europe
customer_keynumber · unique id
5
customer_idtext
12374
contact_nametext
Emily Hill
countrytext
Austria
regiontext
Europe
customer_keynumber · unique id
6
customer_idtext
12384
contact_nametext
Ava Moore
countrytext
Switzerland
regiontext
Europe
customer_keynumber · unique id
7
customer_idtext
12389
contact_nametext
Leo Moore
countrytext
Australia
regiontext
Rest of World
customer_keynumber · unique id
8
customer_idtext
12392
contact_nametext
Lily Moore
countrytext
Australia
regiontext
Rest of World
customer_keynumber · unique id
9
customer_idtext
12404
contact_nametext
Ava Smith
countrytext
Finland
regiontext
Europe
customer_keynumber · unique id
10
customer_idtext
12416
contact_nametext
Sophie Smith
countrytext
Australia
regiontext
Rest of World

first 10 of 848 rows · your queries use all of them

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

dim_product5 columns · 4,902 rows
product_keynumber · unique idproduct_idtextdescriptiontextcategorytextpricedecimal
110002INFLATABLE POLITICAL GLOBEOther Gifts0.85
210002RROBOT PENCIL SHARPNERStationery & Gift Wrap5.17
310080GROOVY CACTUS INFLATABLEOther Gifts0.39
410109BENDY COLOUR PENCILSStationery & Gift Wrap0.42
510120DOGGY RUBBEROther Gifts0.21
610123CHEARTS WRAPPING TAPEStationery & Gift Wrap0.65
710123GARMY CAMO WRAPPING TAPEStationery & Gift Wrap0.65
810124ASPOTS ON RED BOOKCOVER TAPEStationery & Gift Wrap0.42
910124GARMY CAMO BOOKCOVER TAPEStationery & Gift Wrap0.42
1010125MINI FUNKY DESIGN TAPESStationery & Gift Wrap0.85
product_keynumber · unique id
1
product_idtext
10002
descriptiontext
INFLATABLE POLITICAL GLOBE
categorytext
Other Gifts
pricedecimal
0.85
product_keynumber · unique id
2
product_idtext
10002R
descriptiontext
ROBOT PENCIL SHARPNER
categorytext
Stationery & Gift Wrap
pricedecimal
5.17
product_keynumber · unique id
3
product_idtext
10080
descriptiontext
GROOVY CACTUS INFLATABLE
categorytext
Other Gifts
pricedecimal
0.39
product_keynumber · unique id
4
product_idtext
10109
descriptiontext
BENDY COLOUR PENCILS
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
5
product_idtext
10120
descriptiontext
DOGGY RUBBER
categorytext
Other Gifts
pricedecimal
0.21
product_keynumber · unique id
6
product_idtext
10123C
descriptiontext
HEARTS WRAPPING TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.65
product_keynumber · unique id
7
product_idtext
10123G
descriptiontext
ARMY CAMO WRAPPING TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.65
product_keynumber · unique id
8
product_idtext
10124A
descriptiontext
SPOTS ON RED BOOKCOVER TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
9
product_idtext
10124G
descriptiontext
ARMY CAMO BOOKCOVER TAPE
categorytext
Stationery & Gift Wrap
pricedecimal
0.42
product_keynumber · unique id
10
product_idtext
10125
descriptiontext
MINI FUNKY DESIGN TAPES
categorytext
Stationery & Gift Wrap
pricedecimal
0.85

first 10 of 4,902 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. Keep customers with no sales. LEFT JOIN from dim_customer to fact_sales still returns customers with zero lines—an inner join would drop them from a customer report.the pattern

    example.sql
    SELECT c.customer_id, COUNT(s.sale_key) AS lines
    FROM dim_customer c
    LEFT JOIN fact_sales s ON s.customer_key = c.customer_key
    GROUP BY c.customer_id
    ORDER BY lines
    LIMIT 10;
  2. Measure whole days between dates. julianday subtracts two dates and gives a whole-day count—the same idea recency_days uses with a fixed reference date.

    step-2.sql
    SELECT julianday('2011-12-10') - julianday('2011-11-30') AS days;

common mistake

An inner join from customers to sales silently drops customers with no 2011 purchases; a customer report should keep them with zero sales.

challenge 1

Find every customer in dim_customer.

Return customer_id, sales_lines, total_sales (rounded to 2, 0 when none) and recency_days: whole days from the last 2011 order_date to 2011-12-10, NULL with no sales.

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