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

country_codes2 columns · 26 rows
codetext · unique idcountrytext
AEUnited Arab Emirates
ATAustria
AUAustralia
BEBelgium
BHBahrain
BRBrazil
CHSwitzerland
CYCyprus
DEGermany
DKDenmark
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
codetext · unique id
CH
countrytext
Switzerland
codetext · unique id
CY
countrytext
Cyprus
codetext · unique id
DE
countrytext
Germany
codetext · unique id
DK
countrytext
Denmark

first 10 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
712373Harry HillM2010-02-04 10:00:00
812373Harry HillM2010-04-01 10:00:00
912373Harry HillM2011-04-01 10:00:00
1012374Emily HillM2010-04-30 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
source_rownumber · unique id
7
customer_idtext
12373
contact_nametext
Harry Hill
gendertext
M
updated_atdate & time
2010-02-04 10:00:00
source_rownumber · unique id
8
customer_idtext
12373
contact_nametext
Harry Hill
gendertext
M
updated_atdate & time
2010-04-01 10:00:00
source_rownumber · unique id
9
customer_idtext
12373
contact_nametext
Harry Hill
gendertext
M
updated_atdate & time
2011-04-01 10:00:00
source_rownumber · unique id
10
customer_idtext
12374
contact_nametext
Emily Hill
gendertext
M
updated_atdate & time
2010-04-30 10:00:00

first 10 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
12865female1984-02-14
12353NULL1989-06-06
12431female1973-12-28
NAS12447female1989-04-16
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
erp_idtext · unique id
12865
gendertext
female
birth_datedate
1984-02-14
erp_idtext · unique id
12353
gendertext
NULL
birth_datedate
1989-06-06
erp_idtext · unique id
12431
gendertext
female
birth_datedate
1973-12-28
erp_idtext · unique id
NAS12447
gendertext
female
birth_datedate
1989-04-16

first 10 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
12-865AT
12-353BH
12-431BE
12-447BE
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
erp_idtext · unique id
12-865
country_codetext
AT
erp_idtext · unique id
12-353
country_codetext
BH
erp_idtext · unique id
12-431
country_codetext
BE
erp_idtext · unique id
12-447
country_codetext
BE

first 10 of 848 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. 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

Find every keyed CRM customer under the integration contract.

Return customer_id, contact_name, gender, country and birth_date.

challenge-1.sql
 
Output
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