runtime
78 / 88

lesson 78 Warehouse & reporting projects · advanced

Integrate CRM + ERP with source authority

Combine CRM and ERP using clear source ownership rules.

the idea

Integration follows a written contract. Here: keep the latest CRM version per trimmed key (source_row breaks equal timestamps); normalize ERP keys by removing a leading NAS and location keys by removing hyphens; CRM gender wins when it is F or M, ERP gender is the fallback; countries come from the ISO code table; birth dates must be valid and not after 2011-12-31. Every keyed CRM customer is kept.

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

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. Strip NAS from ERP ids. Some erp_id values start with NAS; the contract removes that prefix so ERP keys align with CRM customer_id.the pattern

    example.sql
    SELECT
      erp_id,
      CASE
        WHEN substr(TRIM(erp_id), 1, 3) = 'NAS' THEN substr(TRIM(erp_id), 4)
        ELSE TRIM(erp_id)
      END AS customer_id
    FROM erp_people
    LIMIT 10;
  2. Remove hyphens from location ids. erp_locations uses hyphenated ids; REPLACE lets you join locations to the same normalized customer_id as people.

    step-2.sql
    SELECT erp_id, REPLACE(erp_id, '-', '') AS customer_id, country_code
    FROM erp_locations
    LIMIT 10;
  3. Group ERP gender spellings. ERP uses FEMALE and MALE while CRM uses F and M—you need explicit mapping before COALESCE can pick a fallback.

    step-3.sql
    SELECT UPPER(TRIM(gender)) AS erp_gender, COUNT(*) AS customers
    FROM erp_people
    GROUP BY UPPER(TRIM(gender));

common mistake

COALESCE(crm_gender, erp_gender) only works if sentinel values like 'n/a' and '' are first turned into NULL.

challenge 1

Return customer_id, contact_name, gender, country and birth_date for every keyed CRM customer under the integration contract.

what to return

Columns
customer_idcontact_namegendercountrybirth_date 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.
Integrate CRM + ERP with source authority — Warehouse & reporting projects · Runtime