|
| 1 | +--############################################################################# |
| 2 | +-- Staging Model: src_transactions |
| 3 | +-- Purpose: Clean and deduplicate raw bronze.sales_transactions |
| 4 | +--############################################################################# |
| 5 | + |
| 6 | +WITH source AS ( |
| 7 | + SELECT * |
| 8 | + FROM {{ source('bronze', 'sales_transactions') }} |
| 9 | +), |
| 10 | + |
| 11 | +deduplicated AS ( |
| 12 | + SELECT *, |
| 13 | + ROW_NUMBER() OVER ( |
| 14 | + PARTITION BY transaction_id |
| 15 | + ORDER BY transaction_id |
| 16 | + ) AS _row_flag |
| 17 | + FROM source |
| 18 | + WHERE transaction_id IS NOT NULL |
| 19 | +) |
| 20 | + |
| 21 | +SELECT |
| 22 | + UPPER(TRIM(transaction_id)) AS transaction_id, |
| 23 | + |
| 24 | + CASE WHEN TRY_CAST(order_id AS INT) IS NULL OR LEN(TRIM(order_id)) < 5 THEN NULL ELSE TRY_CAST(order_id AS INT) END AS order_id, |
| 25 | + |
| 26 | + TRY_CAST(order_line_number AS INT) AS order_line_number, |
| 27 | + |
| 28 | + CASE |
| 29 | + WHEN TRIM(order_date) LIKE '[A-Z][a-z][a-z][a-z]% __, ____' THEN TRY_CONVERT(DATE, order_date) |
| 30 | + WHEN TRIM(order_date) LIKE '[A-Z][a-z][a-z] __, ____' THEN TRY_CONVERT(DATE, order_date) |
| 31 | + WHEN TRIM(order_date) LIKE '____-__-__' THEN TRY_CONVERT(DATE, order_date) |
| 32 | + WHEN TRIM(order_date) LIKE '____/__/__' THEN TRY_CONVERT(DATE, order_date) |
| 33 | + WHEN TRIM(order_date) LIKE '__/__/____' |
| 34 | + AND TRY_CONVERT(INT, SUBSTRING(TRIM(order_date), 4, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 101) |
| 35 | + WHEN TRIM(order_date) LIKE '__/__/____' |
| 36 | + AND TRY_CONVERT(INT, LEFT(TRIM(order_date), 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 103) |
| 37 | + WHEN TRIM(order_date) LIKE '__-__-____' |
| 38 | + AND TRY_CONVERT(INT, SUBSTRING(TRIM(order_date), 4, 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 110) |
| 39 | + WHEN TRIM(order_date) LIKE '__-__-____' |
| 40 | + AND TRY_CONVERT(INT, LEFT(TRIM(order_date), 2)) > 12 THEN TRY_CONVERT(DATE, order_date, 105) |
| 41 | + ELSE TRY_CONVERT(DATE, order_date) |
| 42 | + END AS order_date, |
| 43 | + |
| 44 | + order_year, order_month, order_month_name, order_quarter, order_day_of_week, |
| 45 | + TRY_CAST(customer_id AS INT) AS customer_id, |
| 46 | + customer_full_name, customer_first_name, customer_last_name, customer_email, |
| 47 | + customer_phone, customer_city, customer_state, |
| 48 | + CASE WHEN TRY_CAST(customer_zip AS INT) IS NULL OR LEN(customer_zip) != 5 THEN NULL ELSE TRY_CAST(customer_zip AS INT) END AS customer_zip, |
| 49 | + customer_region, customer_segment, customer_gender, customer_age, customer_age_group, |
| 50 | + product_id, product_name, sku, brand, category, sub_category, department, |
| 51 | + quantity_ordered, |
| 52 | + unit_list_price, discount_pct, unit_selling_price, |
| 53 | + line_total_before_tax, tax_rate_pct, tax_amount, line_total_with_tax, |
| 54 | + store_id, store_name, store_city, store_state, store_region, store_type, |
| 55 | + employee_id, employee_name, employee_job_title, |
| 56 | + promo_id, promo_name, |
| 57 | + LOWER(TRIM(sales_channel)) AS sales_channel, |
| 58 | + payment_method, shipping_method, order_status, is_returned, |
| 59 | + cost_price, gross_profit, data_source, record_created_ts, last_modified_ts |
| 60 | +FROM deduplicated |
| 61 | +WHERE _row_flag = 1; |
| 62 | + |
| 63 | + |
0 commit comments