Skip to content

Commit ac3dca5

Browse files
Merge pull request #27 from Ritik574-coder/dbt_branch
feat(dbt): add intermediate transformation in Transactions models and applying business logic
2 parents 23c4abf + d002cd5 commit ac3dca5

4 files changed

Lines changed: 253 additions & 27 deletions

File tree

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
1+
WITH int_transaction_financial AS
2+
(
13
SELECT
24
transaction_id,
35

4-
quantity_ordered,
6+
CASE
7+
WHEN quantity_ordered < 1 THEN NULL
8+
WHEN quantity_ordered > 30 THEN NULL
9+
ELSE quantity_ordered
10+
END as quantity_ordered ,
511

6-
unit_list_price,
7-
discount_pct,
8-
unit_selling_price,
12+
CASE
13+
WHEN unit_list_price IS NULL THEN NULL
14+
ELSE TRY_CONVERT(DECIMAL(10, 2), REPLACE(REPLACE(unit_list_price, '$', ''), ',', ''))
15+
END as unit_list_price,
16+
17+
CASE
18+
WHEN discount_pct IS NULL OR discount_pct < 0 OR discount_pct > 100 THEN NULL
19+
ELSE ROUND(discount_pct, 0)
20+
END as discount_pct,
21+
22+
CASE
23+
WHEN line_total_before_tax IS NULL OR TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(line_total_before_tax, '$', ''), ',', '')) < 0 THEN NULL
24+
ELSE TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(line_total_before_tax, '$', ''), ',', ''))
25+
END as line_total_before_tax,
26+
27+
CASE
28+
WHEN tax_rate_pct IS NULL OR TRY_CONVERT(INT, ROUND(tax_rate_pct, 0)) < 0 OR TRY_CONVERT(INT, ROUND(tax_rate_pct, 0)) > 100 THEN NULL
29+
ELSE TRY_CONVERT(INT, ROUND(tax_rate_pct, 0))
30+
END as tax_rate_pct,
931

10-
line_total_before_tax,
32+
CASE
33+
WHEN tax_amount IS NULL OR TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(tax_amount, '$', ''), ',', '')) < 0 THEN NULL
34+
ELSE TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(tax_amount, '$', ''), ',', ''))
35+
END as tax_amount,
1136

37+
CASE
38+
WHEN line_total_with_tax IS NULL OR TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(line_total_with_tax, '$', ''), ',', '')) < 0 THEN NULL
39+
ELSE TRY_CONVERT(DECIMAL(10,2), REPLACE(REPLACE(line_total_with_tax, '$', ''), ',', ''))
40+
END as line_total_with_tax
41+
FROM {{ ref('stg_transactions') }}
42+
)
43+
SELECT
44+
transaction_id,
45+
quantity_ordered,
46+
unit_list_price,
47+
discount_pct,
48+
ROUND(unit_list_price * (1 - discount_pct/100), 2) as unit_selling_price,
49+
ROUND(quantity_ordered * ROUND(unit_list_price * (1 - discount_pct/100.0), 2),2) AS line_total_before_tax,
1250
tax_rate_pct,
1351
tax_amount,
14-
15-
line_total_with_tax
16-
FROM {{ ref('stg_transactions') }} ;
52+
ROUND(quantity_ordered * ROUND(unit_list_price * (1 - discount_pct/100.0), 2),2) + tax_amount AS line_total_with_tax
53+
FROM int_transaction_financial ;
Lines changed: 165 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,167 @@
1-
SELECT
2-
transaction_id,
1+
WITH transaction_fulfillment AS
2+
(
3+
SELECT
4+
transaction_id,
5+
6+
CASE
7+
WHEN order_date LIKE '[A-Z][a-z][a-z][a-z]% __, ____' THEN TRY_CONVERT(DATE , order_date)
8+
WHEN order_date LIKE '[A-Z][a-z][a-z] __, ____' THEN TRY_CONVERT(DATE , order_date)
9+
WHEN order_date LIKE '____/__/__' THEN TRY_CONVERT(DATE , order_date)
10+
WHEN order_date LIKE '____-__-__' THEN TRY_CONVERT(DATE , order_date)
11+
WHEN order_date LIKE '__/__/____' AND TRY_CONVERT(INT, SUBSTRING(order_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 101)
12+
WHEN order_date LIKE '__-__-____' AND TRY_CONVERT(INT, SUBSTRING(order_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 110)
13+
WHEN order_date LIKE '__/__/____' AND TRY_CONVERT(INT, LEFT(order_date, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 103)
14+
WHEN order_date LIKE '__-__-____' AND TRY_CONVERT(INT, LEFT(order_date, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 105)
15+
ELSE TRY_CONVERT(DATE, TRIM(order_date), 101)
16+
END as order_date,
17+
18+
order_month,
19+
20+
CASE
21+
WHEN ship_date LIKE '[A-Z][a-z][a-z][a-z]% __, ____' THEN TRY_CONVERT(DATE , ship_date)
22+
WHEN ship_date LIKE '[A-Z][a-z][a-z] __, ____' THEN TRY_CONVERT(DATE , ship_date)
23+
WHEN ship_date LIKE '____/__/__' THEN TRY_CONVERT(DATE , ship_date)
24+
WHEN ship_date LIKE '____-__-__' THEN TRY_CONVERT(DATE , ship_date)
25+
WHEN ship_date LIKE '__/__/____' AND TRY_CONVERT(INT, SUBSTRING(ship_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, ship_date, 101)
26+
WHEN ship_date LIKE '__-__-____' AND TRY_CONVERT(INT, SUBSTRING(ship_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, ship_date, 110)
27+
WHEN ship_date LIKE '__/__/____' AND TRY_CONVERT(INT, LEFT(ship_date, 2)) > 12 THEN TRY_CONVERT(DATE, ship_date, 103)
28+
WHEN ship_date LIKE '__-__-____' AND TRY_CONVERT(INT, LEFT(ship_date, 2)) > 12 THEN TRY_CONVERT(DATE, ship_date, 105)
29+
ELSE TRY_CONVERT(DATE, TRIM(ship_date), 101)
30+
END as ship_date,
31+
32+
CASE
33+
WHEN delivery_date LIKE '[A-Z][a-z][a-z][a-z]% __, ____' THEN TRY_CONVERT(DATE , delivery_date)
34+
WHEN delivery_date LIKE '[A-Z][a-z][a-z] __, ____' THEN TRY_CONVERT(DATE , delivery_date)
35+
WHEN delivery_date LIKE '____/__/__' THEN TRY_CONVERT(DATE , delivery_date)
36+
WHEN delivery_date LIKE '____-__-__' THEN TRY_CONVERT(DATE , delivery_date)
37+
WHEN delivery_date LIKE '__/__/____' AND TRY_CONVERT(INT, SUBSTRING(delivery_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, delivery_date, 101)
38+
WHEN delivery_date LIKE '__-__-____' AND TRY_CONVERT(INT, SUBSTRING(delivery_date, 4, 2)) > 12 THEN TRY_CONVERT(DATE, delivery_date, 110)
39+
WHEN delivery_date LIKE '__/__/____' AND TRY_CONVERT(INT, LEFT(delivery_date, 2)) > 12 THEN TRY_CONVERT(DATE, delivery_date, 103)
40+
WHEN delivery_date LIKE '__-__-____' AND TRY_CONVERT(INT, LEFT(delivery_date, 2)) > 12 THEN TRY_CONVERT(DATE, delivery_date, 105)
41+
ELSE TRY_CONVERT(DATE, TRIM(delivery_date), 101)
42+
END as delivery_date,
43+
44+
CASE
45+
WHEN TRIM(LOWER(shipping_method)) IN ('pickup', 'in-store pickup') THEN 'Store Pickup'
46+
WHEN TRIM(LOWER(shipping_method)) IN ('overnight', 'overnight shipping') THEN 'Overnight Shipping'
47+
WHEN TRIM(LOWER(shipping_method)) IN ('same day', 'same day delivery') THEN 'Same Day Delivery'
48+
WHEN TRIM(LOWER(shipping_method)) IN ('express', 'express shipping') THEN 'Express Shipping'
49+
WHEN TRIM(LOWER(shipping_method)) IN ('standard', 'standard shipping') THEN 'Standard Shipping'
50+
WHEN TRIM(LOWER(shipping_method)) IN ('free ship', 'free shipping') THEN 'Free Shipping'
51+
ELSE TRIM(shipping_method)
52+
END AS shipping_method,
53+
54+
CASE
55+
WHEN TRIM(LOWER(sales_channel)) IN ('app', 'mobile', 'mobile app') THEN 'Mobile App'
56+
WHEN TRIM(LOWER(sales_channel)) IN ('store', 'in store', 'in-store') THEN 'In Store'
57+
WHEN TRIM(LOWER(sales_channel)) IN ('online', 'web') THEN 'Website'
58+
WHEN TRIM(LOWER(sales_channel)) IN ('phone') THEN 'Phone Call'
59+
WHEN TRIM(LOWER(sales_channel)) IN ('catalog') THEN 'Catalog'
60+
ELSE 'Unknown'
61+
END AS sales_channel,
62+
63+
CASE TRIM(LOWER(payment_method))
64+
WHEN 'debit card' THEN 'Debit Card'
65+
WHEN 'credit card' THEN 'Credit Card'
66+
WHEN 'google pay' THEN 'Google Pay'
67+
WHEN 'apple pay' THEN 'Apple Pay'
68+
WHEN 'bank transfer' THEN 'Bank Transfer'
69+
WHEN 'buy now pay later' THEN 'Buy Now Pay Later'
70+
WHEN 'bnpl' THEN 'Buy Now Pay Later'
71+
WHEN 'gift card' THEN 'Gift Card'
72+
WHEN 'paypal' THEN 'PayPal'
73+
WHEN 'cash' THEN 'Cash'
74+
ELSE TRIM(payment_method)
75+
END AS payment_method,
76+
77+
TRY_CONVERT(DATE, record_created_ts) as record_created,
78+
TRY_CONVERT(DATE, last_modified_ts ) as last_modified
79+
FROM {{ ref('stg_transactions') }}
80+
),
81+
order_date_cleaning AS
82+
(
83+
SELECT
84+
transaction_id,
385

4-
ship_date,
5-
delivery_date,
86+
CASE
87+
WHEN order_month != MONTH(order_date) THEN DATEFROMPARTS(YEAR(order_date), DAY(order_date), MONTH(order_date))
88+
ELSE order_date
89+
END AS order_date,
690

7-
shipping_method,
8-
sales_channel,
9-
payment_method
10-
FROM {{ ref('stg_transactions') }} ;
91+
ship_date,
92+
delivery_date,
93+
94+
shipping_method,
95+
sales_channel,
96+
payment_method,
97+
98+
CASE
99+
WHEN record_created IS NULL
100+
OR record_created NOT LIKE '____-__-__'
101+
OR TRY_CONVERT(DATE, record_created) IS NULL
102+
OR YEAR(record_created) < 2019 THEN NULL
103+
ELSE record_created
104+
END as record_created,
105+
106+
CASE
107+
WHEN last_modified IS NULL
108+
OR last_modified NOT LIKE '____-__-__'
109+
OR TRY_CONVERT(DATE, last_modified) IS NULL
110+
OR YEAR(last_modified) < 2019 THEN NULL
111+
ELSE last_modified
112+
END as last_modified
113+
114+
FROM transaction_fulfillment
115+
),
116+
ship_date_cleaning AS
117+
(
118+
SELECT
119+
transaction_id,
120+
order_date ,
121+
122+
CASE
123+
WHEN DATEDIFF(DAY ,order_date, ship_date) < 0 THEN DATEFROMPARTS(YEAR(ship_date), DAY(ship_date), MONTH(ship_date))
124+
WHEN DATEDIFF(DAY ,order_date, ship_date) > 15 THEN DATEFROMPARTS(YEAR(ship_date), DAY(ship_date), MONTH(ship_date))
125+
WHEN ship_date IS NULL THEN DATEADD(DAY, 7, order_date)
126+
ELSE ship_date
127+
END as ship_date ,
128+
129+
delivery_date,
130+
shipping_method,
131+
sales_channel,
132+
payment_method,
133+
record_created,
134+
last_modified
135+
FROM order_date_cleaning
136+
),
137+
int_transaction_fulfillment AS
138+
(
139+
SELECT
140+
transaction_id,
141+
order_date,
142+
ship_date,
143+
144+
CASE
145+
WHEN DATEDIFF(DAY, ship_date , delivery_date) > 17 THEN DATEFROMPARTS(YEAR(delivery_date), DAY(delivery_date), MONTH(delivery_date))
146+
WHEN DATEDIFF(DAY, ship_date , delivery_date) < -15 THEN DATEFROMPARTS(YEAR(delivery_date), DAY(delivery_date), MONTH(delivery_date))
147+
ELSE delivery_date
148+
END as delivery_date,
149+
150+
shipping_method,
151+
sales_channel,
152+
payment_method,
153+
record_created,
154+
last_modified
155+
FROM ship_date_cleaning
156+
)
157+
SELECT
158+
transaction_id,
159+
order_date,
160+
ship_date,
161+
delivery_date,
162+
shipping_method,
163+
sales_channel,
164+
payment_method,
165+
record_created,
166+
last_modified
167+
FROM int_transaction_fulfillment;
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
SELECT
22
transaction_id,
33
order_id,
4-
order_line_number,
5-
64
customer_id,
75
product_id,
86
store_id,
97
employee_id,
10-
promo_id,
11-
12-
order_date,
13-
record_created_ts,
14-
last_modified_ts
8+
CASE
9+
WHEN order_line_number < 1 THEN NULL
10+
WHEN order_line_number > 20 THEN NULL
11+
ELSE order_line_number
12+
END as order_line_number
1513
FROM {{ ref('stg_transactions') }} ;

models/intermediate/Transactions/int_transaction_status.sql

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,43 @@ SELECT
22
transaction_id,
33

44
promo_id,
5-
promo_name,
65

7-
order_status,
8-
is_returned,
9-
data_source
6+
CASE TRIM(LOWER(promo_name))
7+
WHEN 'winter clearance' THEN 'Winter Clearance'
8+
WHEN 'bundle deal' THEN 'Bundle Deal'
9+
WHEN 'no promo' THEN 'No Promo'
10+
WHEN 'flash sale' THEN 'Flash Sale'
11+
WHEN 'black friday' THEN 'Black Friday'
12+
WHEN 'holiday special' THEN 'Holiday Special'
13+
WHEN 'weekend deal' THEN 'Weekend Deal'
14+
WHEN 'loyalty reward' THEN 'Loyalty Reward'
15+
WHEN 'cyber monday' THEN 'Cyber Monday'
16+
ELSE TRIM(promo_name)
17+
END AS promo_name,
18+
19+
CASE TRIM(LOWER(order_status))
20+
WHEN 'pending' THEN 'Pending'
21+
WHEN 'processing' THEN 'Processing'
22+
WHEN 'shipped' THEN 'Shipped'
23+
WHEN 'delivered' THEN 'Delivered'
24+
WHEN 'returned' THEN 'Returned'
25+
WHEN 'cancelled' THEN 'Cancelled'
26+
ELSE TRIM(order_status)
27+
END AS order_status,
28+
29+
CASE
30+
WHEN TRIM(LOWER(is_returned)) IN ('yes', 'y', 'true', '1') THEN 'True'
31+
WHEN TRIM(LOWER(is_returned)) IN ('no', 'n', 'false', '0') THEN 'False'
32+
ELSE 'Unknown'
33+
END AS is_returned,
34+
35+
CASE TRIM(LOWER(data_source))
36+
WHEN 'crm' THEN 'CRM'
37+
WHEN 'web' THEN 'Web'
38+
WHEN 'pos' THEN 'POS'
39+
WHEN 'manual' THEN 'Manual'
40+
WHEN 'erp' THEN 'ERP'
41+
ELSE 'Unknown'
42+
END AS data_source
43+
1044
FROM {{ ref('stg_transactions') }} ;

0 commit comments

Comments
 (0)