-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0_create_view.sql
More file actions
28 lines (28 loc) · 872 Bytes
/
Copy path0_create_view.sql
File metadata and controls
28 lines (28 loc) · 872 Bytes
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
CREATE OR REPLACE VIEW cohort_analysis AS
WITH customer_revenue AS (
SELECT
s.customerkey,
s.orderdate,
SUM(s.quantity * s.netprice * s.exchangerate) AS total_net_revenue,
COUNT(s.orderkey) AS num_orders,
MAX(c.countryfull) AS countryfull,
MAX(c.age) AS age,
MAX(c.givenname) AS givenname,
MAX(c.surname) AS surname
FROM sales s
INNER JOIN customer c ON c.customerkey = s.customerkey
GROUP BY
s.customerkey,
s.orderdate
)
SELECT
customerkey,
orderdate,
total_net_revenue,
num_orders,
countryfull,
age,
CONCAT(TRIM(givenname), ' ', TRIM(surname)) AS cleaned_name,
MIN(orderdate) OVER (PARTITION BY customerkey) AS first_purchase_date,
EXTRACT(YEAR FROM MIN(orderdate) OVER (PARTITION BY customerkey)) AS cohort_year
FROM customer_revenue cr;