-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfm_query.sql
More file actions
30 lines (30 loc) · 927 Bytes
/
rfm_query.sql
File metadata and controls
30 lines (30 loc) · 927 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
rfm_query = """
WITH
-- Find the snapshot date, which is one day after the last transaction in the whole dataset
snapshot AS (
SELECT date(MAX(InvoiceDate), '+1 day') AS snapshot_date
FROM transactions
),
-- Calculate raw RFM metrics for each customer
rfm_metrics AS (
SELECT
CustomerID,
MAX(date(InvoiceDate)) AS last_purchase_date,
COUNT(DISTINCT InvoiceNo) AS Frequency,
SUM(Quantity * UnitPrice) AS MonetaryValue
FROM
transactions
WHERE
CustomerID IS NOT NULL AND Quantity > 0 AND UnitPrice > 0
GROUP BY
CustomerID
)
-- Final SELECT: Calculate Recency by comparing to the snapshot_date
SELECT
m.CustomerID,
(julianday(s.snapshot_date) - julianday(m.last_purchase_date)) AS Recency,
m.Frequency,
m.MonetaryValue
FROM
rfm_metrics m, snapshot s;
"""