runtime
88 / 88

lesson 88 Warehouse & reporting projects · advanced

Capstone: integrated customers to trusted sales

Combine integration and quality checks into trusted sales reporting.

the idea

The capstone chains the warehouse steps: integrate CRM and ERP customers under the contract (lesson 77), build quality-gated sales from Bronze (lesson 71), then report trusted sales at the integrated customer grain. Each step is materialized so it can be inspected and reconciled.

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.

country_codes2 columns · 26 rows
codetext · unique idcountrytext
AEUnited Arab Emirates
ATAustria
AUAustralia
BEBelgium
BHBahrain
BRBrazil
codetext · unique id
AE
countrytext
United Arab Emirates
codetext · unique id
AT
countrytext
Austria
codetext · unique id
AU
countrytext
Australia
codetext · unique id
BE
countrytext
Belgium
codetext · unique id
BH
countrytext
Bahrain
codetext · unique id
BR
countrytext
Brazil

first 6 of 26 rows · your queries use all of them

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

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

bronze_sales6 columns · 2,526 rows
row_idnumber · unique idcustomer_idtextproduct_idtextquantitytextsalestextorder_datedate
12302715687233012439.602011-12-01
1230281568722623317.852011-12-01
1230291568720970415.002011-12-01
1230301568723562617.342011-12-01
1230311568771477415.802011-12-01
123032 15687 23407219.902011-12-01
row_idnumber · unique id
123027
customer_idtext
15687
product_idtext
23301
quantitytext
24
salestext
39.60
order_datedate
2011-12-01
row_idnumber · unique id
123028
customer_idtext
15687
product_idtext
22623
quantitytext
3
salestext
17.85
order_datedate
2011-12-01
row_idnumber · unique id
123029
customer_idtext
15687
product_idtext
20970
quantitytext
4
salestext
15.00
order_datedate
2011-12-01
row_idnumber · unique id
123030
customer_idtext
15687
product_idtext
23562
quantitytext
6
salestext
17.34
order_datedate
2011-12-01
row_idnumber · unique id
123031
customer_idtext
15687
product_idtext
71477
quantitytext
4
salestext
15.80
order_datedate
2011-12-01
row_idnumber · unique id
123032
customer_idtext
15687
product_idtext
23407
quantitytext
2
salestext
19.90
order_datedate
2011-12-01

first 6 of 2,526 rows · your queries use all of them

crm_people5 columns · 1,707 rows
source_rownumber · unique idcustomer_idtextcontact_nametextgendertextupdated_atdate & time
112347Arthur Turnern/a2010-10-31 10:00:00
212347Arthur Turnern/a2010-11-01 10:00:00
312353Harry TurnerM2010-10-27 10:00:00
412359Theo TurnerM2009-12-05 10:00:00
512359Theo TurnerM2010-01-01 10:00:00
612359Theo TurnerM2011-11-01 10:00:00
source_rownumber · unique id
1
customer_idtext
12347
contact_nametext
Arthur Turner
gendertext
n/a
updated_atdate & time
2010-10-31 10:00:00
source_rownumber · unique id
2
customer_idtext
12347
contact_nametext
Arthur Turner
gendertext
n/a
updated_atdate & time
2010-11-01 10:00:00
source_rownumber · unique id
3
customer_idtext
12353
contact_nametext
Harry Turner
gendertext
M
updated_atdate & time
2010-10-27 10:00:00
source_rownumber · unique id
4
customer_idtext
12359
contact_nametext
Theo Turner
gendertext
M
updated_atdate & time
2009-12-05 10:00:00
source_rownumber · unique id
5
customer_idtext
12359
contact_nametext
Theo Turner
gendertext
M
updated_atdate & time
2010-01-01 10:00:00
source_rownumber · unique id
6
customer_idtext
12359
contact_nametext
Theo Turner
gendertext
M
updated_atdate & time
2011-11-01 10:00:00

first 6 of 1,707 rows · your queries use all of them

erp_people3 columns · 848 rows
erp_idtext · unique idgendertextbirth_datedate
12389female1978-06-14
12392Female1981-09-17
12416Male2031-05-01
12373Male1962-02-26
12374Male1963-03-27
12817female1983-02-22
erp_idtext · unique id
12389
gendertext
female
birth_datedate
1978-06-14
erp_idtext · unique id
12392
gendertext
Female
birth_datedate
1981-09-17
erp_idtext · unique id
12416
gendertext
Male
birth_datedate
2031-05-01
erp_idtext · unique id
12373
gendertext
Male
birth_datedate
1962-02-26
erp_idtext · unique id
12374
gendertext
Male
birth_datedate
1963-03-27
erp_idtext · unique id
12817
gendertext
female
birth_datedate
1983-02-22

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

erp_locations2 columns · 848 rows
erp_idtext · unique idcountry_codetext
12-389AU
12-392AU
12-416AU
12-373AT
12-374AT
12-817AT
erp_idtext · unique id
12-389
country_codetext
AU
erp_idtext · unique id
12-392
country_codetext
AU
erp_idtext · unique id
12-416
country_codetext
AU
erp_idtext · unique id
12-373
country_codetext
AT
erp_idtext · unique id
12-374
country_codetext
AT
erp_idtext · unique id
12-817
country_codetext
AT

first 6 of 848 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. Size the raw Bronze batch. bronze_lines and distinct customers describe December sales before quality gates or integration—your capstone checkpoints start here.the pattern

    example.sql
    SELECT
      COUNT(*) AS bronze_lines,
      COUNT(DISTINCT TRIM(customer_id)) AS customers
    FROM bronze_sales;
  2. Count CRM customers missing ERP. The integration contract keeps every keyed CRM row; without_erp shows how many have no matching ERP person.

    step-2.sql
    WITH crm AS (
      SELECT customer_id, contact_name, crm_gender
      FROM (
        SELECT
          TRIM(customer_id) AS customer_id,
          TRIM(contact_name) AS contact_name,
          CASE UPPER(TRIM(gender))
            WHEN 'F' THEN 'Female'
            WHEN 'M' THEN 'Male'
          END AS crm_gender,
          ROW_NUMBER() OVER (PARTITION BY TRIM(customer_id) ORDER BY updated_at DESC, source_row DESC) AS rn
        FROM crm_people
        WHERE customer_id IS NOT NULL AND TRIM(customer_id) <> ''
      )
      WHERE rn = 1
    ), erp AS (
      SELECT
        CASE
          WHEN substr(TRIM(erp_id), 1, 3) = 'NAS'
            THEN substr(TRIM(erp_id), 4)
          ELSE TRIM(erp_id)
        END AS customer_id,
        CASE UPPER(TRIM(gender))
          WHEN 'FEMALE' THEN 'Female'
          WHEN 'MALE' THEN 'Male'
        END AS erp_gender,
        CASE
          WHEN date(birth_date, '+0 days') = birth_date
            AND birth_date <= '2011-12-31'
            THEN birth_date
        END AS birth_date
      FROM erp_people
    ), loc AS (
      SELECT REPLACE(TRIM(erp_id), '-', '') AS customer_id, k.country
      FROM erp_locations e
      LEFT JOIN country_codes k ON k.code = UPPER(TRIM(e.country_code))
    )
    SELECT
      COUNT(*) AS crm_customers,
      SUM(erp.customer_id IS NULL) AS without_erp
    FROM crm
    LEFT JOIN erp ON erp.customer_id = crm.customer_id;
  3. Map location codes to countries. erp_locations joins country_codes so integrated customers get a resolved country, not raw ISO text alone.

    step-3.sql
    SELECT k.country, COUNT(*) AS locations
    FROM erp_locations e
    LEFT JOIN country_codes k ON k.code = UPPER(TRIM(e.country_code))
    GROUP BY k.country
    ORDER BY locations DESC;

common mistake

Joining Bronze sales to integrated customers before applying the quality gates lets invalid lines into the report.

challenge 1

Create TEMP table integrated_customers (lesson 77 contract) and TEMP table built_sales (lesson 71 quality gates: customer_key, product_key, quantity, sales, order_date).

Return customer_id, contact_name, country, sales_lines and total_sales (rounded to 2) for integrated customers with at least one accepted December line.

what to return

Columns
customer_idcontact_namecountrysales_linestotal_sales in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Capstone: integrated customers to trusted sales — Warehouse & reporting projects · Runtime