1-- Trung bình số touchpoints trước Purchase đầu tiên
2WITH first_purchase AS (
3 SELECT
4 customer_id,
5 MIN(touchpoint_date) AS first_purchase_date
6 FROM fact_touchpoint
7 WHERE action = 'Purchase'
8 GROUP BY customer_id
9),
10pre_purchase_touches AS (
11 SELECT
12 t.customer_id,
13 COUNT(*) AS touch_count,
14 COUNT(DISTINCT t.channel) AS channels_used
15 FROM fact_touchpoint t
16 JOIN first_purchase fp ON t.customer_id = fp.customer_id
17 WHERE t.touchpoint_date <= fp.first_purchase_date
18 GROUP BY t.customer_id
19)
20SELECT
21 ROUND(AVG(touch_count), 1) AS avg_touches_before_purchase,
22 ROUND(AVG(channels_used), 1) AS avg_channels_used,
23 MIN(touch_count) AS min_touches,
24 MAX(touch_count) AS max_touches
25FROM pre_purchase_touches;