forked from kpaganopoulos/dma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery2b.sql
More file actions
34 lines (31 loc) · 829 Bytes
/
query2b.sql
File metadata and controls
34 lines (31 loc) · 829 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
29
30
31
32
33
34
CREATE VIEW Recency AS
(
SELECT cust_id, MAX(orderdate) AS last_purchase
FROM dmefextractlinesv01
GROUP BY cust_id, orderdate
ORDER BY last_purchase DESC
)
CREATE VIEW Frequency AS
(
SELECT cust_id, COUNT(ordernum) AS total_purchases
FROM dmefextractlinesv01
GROUP BY cust_id
ORDER BY total_purchases DESC
)
CREATE VIEW Monetary AS
(
SELECT cust_id, ROUND(AVG(linedollars)) AS avg_spend
FROM dmefextractlinesv01
GROUP BY cust_id
ORDER BY avg_spend DESC
)
SELECT Recency.cust_id, last_purchase, total_purchases, avg_spend,
NTILE(5) OVER (ORDER BY last_purchase) AS R,
NTILE(5) OVER (ORDER BY total_purchases) AS F,
NTILE(5) OVER (ORDER BY avg_spend) AS M
FROM Recency
INNER JOIN Frequency
ON Recency.cust_id = Frequency.cust_id
INNER JOIN Monetary
ON Frequency.cust_id = Monetary.cust_id
ORDER BY R DESC, F DESC, M DESC