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

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

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
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

first 6 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
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

first 6 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
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

first 6 of 4,902 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not 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

For 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.

what to return

Columns
customer_idsales_linestotal_salesrecency_days in this order
Rows
any order
challenge-1.sql
 
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