File size: 1,429 Bytes
f19868d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- Footfall & conversion analysis
-- Conversion = transactions / visitors. Basket value = revenue / transactions.

-- name: branch_conversion
WITH rev AS (
    SELECT branch_id, SUM(revenue) AS revenue FROM sales GROUP BY branch_id
)
SELECT b.branch_id,
       b.branch_name,
       SUM(f.visitors)                                         AS visitors,
       SUM(f.transactions)                                     AS transactions,
       ROUND(100.0 * SUM(f.transactions) / SUM(f.visitors), 1) AS conversion_pct,
       ROUND(rev.revenue / SUM(f.transactions), 2)             AS avg_basket
FROM footfall f
JOIN branches b ON b.branch_id = f.branch_id
JOIN rev ON rev.branch_id = f.branch_id
GROUP BY b.branch_id
ORDER BY visitors DESC;

-- name: weekday_pattern
SELECT CASE strftime('%w', date)
           WHEN '1' THEN '1-Mon' WHEN '2' THEN '2-Tue' WHEN '3' THEN '3-Wed'
           WHEN '4' THEN '4-Thu' WHEN '5' THEN '5-Fri' WHEN '6' THEN '6-Sat'
           ELSE '7-Sun' END                                  AS weekday,
       ROUND(AVG(visitors), 0)                               AS avg_visitors,
       ROUND(100.0 * SUM(transactions) / SUM(visitors), 1)   AS conversion_pct
FROM footfall
GROUP BY weekday
ORDER BY weekday;

-- name: weekly_trend
SELECT strftime('%Y-W%W', date) AS week,
       SUM(visitors)            AS visitors,
       SUM(transactions)        AS transactions
FROM footfall
GROUP BY week
ORDER BY week;