-
Notifications
You must be signed in to change notification settings - Fork 27
/
attribute_checkouts.sql
46 lines (46 loc) · 1.21 KB
/
attribute_checkouts.sql
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
INSERT INTO
attributed_checkouts
SELECT
checkout_id,
user_name,
click_id,
product_id,
payment_method,
total_amount,
shipping_address,
billing_address,
user_agent,
ip_address,
checkout_time,
click_time
FROM
(
SELECT
co.checkout_id,
u.username AS user_name,
cl.click_id,
co.product_id,
co.payment_method,
co.total_amount,
co.shipping_address,
co.billing_address,
co.user_agent,
co.ip_address,
co.datetime_occured AS checkout_time,
cl.datetime_occured AS click_time,
ROW_NUMBER() OVER (
PARTITION BY cl.user_id,
cl.product_id
ORDER BY
cl.datetime_occured
) AS rn
FROM
checkouts AS co
JOIN users FOR SYSTEM_TIME AS OF co.processing_time AS u ON co.user_id = u.id
LEFT JOIN clicks AS cl ON co.user_id = cl.user_id
AND co.product_id = cl.product_id
AND co.datetime_occured BETWEEN cl.datetime_occured
AND cl.datetime_occured + INTERVAL '1' HOUR
)
WHERE
rn = 1;