Skip to content

Commit cd0abfb

Browse files
Merge pull request #18 from Ritik574-coder/dbt_branch
feat(intermediate): add customer contact cleaning and validation model
2 parents cdd0e7c + 189513e commit cd0abfb

2 files changed

Lines changed: 111 additions & 3 deletions

File tree

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,74 @@
1+
-- ============================================================================
2+
-- Model: int_customers_contact
3+
--
4+
-- Purpose:
5+
-- Standardizes customer contact information by cleaning and validating
6+
-- email addresses, phone numbers, and preferred communication channels.
7+
-- This model corrects common email domain typographical errors, formats
8+
-- phone numbers into a consistent structure, and normalizes customer
9+
-- communication preferences for reliable downstream analytical models.
10+
--
11+
-- Maintainer: Ritik
12+
-- ============================================================================
113
SELECT
214
customer_id,
3-
email,
4-
phone,
5-
preferred_channel
15+
16+
CASE
17+
WHEN email IS NULL OR TRIM(email) = '' THEN 'Unknown'
18+
WHEN TRIM(LOWER(email)) NOT LIKE '%@%' THEN 'Unknown'
19+
WHEN PATINDEX('%@%@%', TRIM(LOWER(email))) > 0 THEN
20+
LEFT(TRIM(LOWER(email)),CHARINDEX('@', TRIM(LOWER(email))) - 1)
21+
+ '@' +
22+
REPLACE(
23+
SUBSTRING(
24+
TRIM(LOWER(email)),
25+
CHARINDEX('@', TRIM(LOWER(email))) + 1,
26+
LEN(email)),'@','')
27+
ELSE
28+
CONCAT(
29+
LEFT(TRIM(LOWER(email)),CHARINDEX('@', TRIM(LOWER(email))) - 1), '@',
30+
CASE
31+
WHEN RIGHT(
32+
TRIM(LOWER(email)),
33+
LEN(TRIM(email)) - CHARINDEX('@', TRIM(email))) = 'yahoocom' THEN 'yahoo.com'
34+
WHEN RIGHT(
35+
TRIM(LOWER(email)),
36+
LEN(TRIM(email)) - CHARINDEX('@', TRIM(email))) = 'iclod.com' THEN 'icloud.com'
37+
WHEN RIGHT(
38+
TRIM(LOWER(email)),
39+
LEN(TRIM(email)) - CHARINDEX('@', TRIM(email))) = 'outook.com' THEN 'outlook.com'
40+
WHEN RIGHT(
41+
TRIM(LOWER(email)),
42+
LEN(TRIM(email)) - CHARINDEX('@', TRIM(email))) = 'ahoo.com' THEN 'yahoo.com'
43+
ELSE RIGHT(
44+
TRIM(LOWER(email)),
45+
LEN(TRIM(email)) - CHARINDEX('@', TRIM(email)))
46+
END
47+
)
48+
END AS email,
49+
50+
CASE
51+
WHEN TRIM(phone) LIKE '+1__________' THEN CONCAT('+1 (', SUBSTRING(TRIM(phone), 3, 3), ') ', SUBSTRING(TRIM(phone), 6, 3),'-', SUBSTRING(TRIM(phone),9,4))
52+
WHEN TRIM(phone) LIKE '__________' THEN CONCAT('+1 (', SUBSTRING(TRIM(phone), 1 ,3), ') ', SUBSTRING(TRIM(phone), 4 ,3), '-', SUBSTRING(TRIM(phone), 7, 4))
53+
WHEN TRIM(phone) LIKE '___-___-____' THEN CONCAT('+1 (', SUBSTRING(TRIM(phone), 1, 3), ') ', SUBSTRING(TRIM(phone), 5, 3), SUBSTRING(TRIM(phone), 8 ,5))
54+
WHEN TRIM(phone) LIKE '___.___.____' THEN CONCAT('+1 (', SUBSTRING(TRIM(phone), 1, 3), ') ', SUBSTRING(TRIM(phone), 5, 3), '-', SUBSTRING(TRIM(phone),9, 4))
55+
WHEN TRIM(phone) LIKE '(___) ___-____' THEN CONCAT('+1 ', SUBSTRING(TRIM(phone), 1, 14))
56+
WHEN TRIM(phone) IS NULL OR TRIM(phone) = '' THEN 'Unknown'
57+
ELSE 'Unknown'
58+
END as phone,
59+
60+
CASE TRIM(LOWER(preferred_channel))
61+
WHEN 'app' THEN 'Mobile App'
62+
WHEN 'mobile app' THEN 'Mobile App'
63+
WHEN 'mobile' THEN 'Mobile App'
64+
WHEN 'in store' THEN 'In Store'
65+
WHEN 'in-store' THEN 'In Store'
66+
WHEN 'store' THEN 'In Store'
67+
WHEN 'catalog' THEN 'Catalog'
68+
WHEN 'online' THEN 'Website'
69+
WHEN 'web' THEN 'Website'
70+
WHEN 'phone' THEN 'Phone Call'
71+
ELSE 'Unknown'
72+
END as preferred_channel
73+
674
FROM {{ ref('stg_customers') }} ;

models/intermediate/_int__retail_models.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,46 @@ models:
5151
tests:
5252
- not_null
5353

54+
####################################################################################
55+
####################### Model: int_customers_contact ###############################
56+
####################################################################################
57+
- name: int_customers_contact
58+
description: |
59+
Cleans and standardizes customer contact information for downstream
60+
analytical models. This model validates and normalizes email addresses,
61+
formats phone numbers into a consistent structure, and standardizes
62+
preferred communication channels to improve data quality and reporting.
63+
64+
columns:
65+
- name: email
66+
description: |
67+
Standardized customer email address. Invalid or missing email
68+
addresses are replaced with 'Unknown'. Common domain spelling
69+
mistakes are automatically corrected.
70+
tests:
71+
- not_null
72+
73+
- name: phone
74+
description: |
75+
Standardized customer phone number formatted as
76+
+1 (XXX) XXX-XXXX. Invalid or missing phone numbers are
77+
replaced with 'Unknown'.
78+
tests:
79+
- not_null
80+
81+
- name: preferred_channel
82+
description: |
83+
Standardized customer preferred communication channel.
84+
Source values are normalized into business-friendly categories.
85+
tests:
86+
- not_null
87+
- accepted_values:
88+
arguments:
89+
values:
90+
['Mobile App', 'In Store', 'Catalog', 'Website', 'Phone Call', 'Unknown']
91+
92+
93+
5494
####################################################################################
5595
####################### Model: int_customer_location ###############################
5696
####################################################################################

0 commit comments

Comments
 (0)