-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathprep__fact_purchases.sql
More file actions
60 lines (52 loc) · 1.67 KB
/
prep__fact_purchases.sql
File metadata and controls
60 lines (52 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{{ config(
alias = 'purchases',
materialized = 'view',
tags=["prep","fact"]
) }}
WITH source AS (
SELECT
-- Convert your transaction_timestamp to date/hour/timestamp
TO_DATE(transaction_timestamp) AS event_date,
EXTRACT(HOUR FROM transaction_timestamp) AS event_hour,
CAST(transaction_timestamp AS TIMESTAMP) AS event_timestamp,
-- Rename _id to _key, as we use _key for natural key and _id for surrogate id
store_id AS store_key,
-- Product info
product_id AS product_key,
product_name,
-- Fact metrics
CAST(quantity AS NUMBER(10,0)) AS quantity,
CAST(unit_price AS NUMBER(10,2)) AS unit_price,
CAST(extended_price AS NUMBER(10,2)) AS extended_price,
payment_method AS payment_method,
-- Original ID (for partitioning dedup logic)
purchase_id AS purchase_key
FROM {{ source('seed', 'purchases') }}
),
standardize AS (
SELECT
event_date,
event_hour,
event_timestamp,
store_key,
product_key,
product_name,
quantity,
unit_price,
extended_price,
payment_method,
purchase_key
FROM source
),
deduplicated AS (
SELECT *
FROM standardize
-- Keep only the most recent row per partition (if duplicates exist).
QUALIFY ROW_NUMBER() OVER (
PARTITION BY
purchase_key
ORDER BY event_timestamp DESC
) = 1
)
SELECT *
FROM deduplicated