-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_queries.sql
More file actions
99 lines (75 loc) · 2.13 KB
/
Copy pathanalysis_queries.sql
File metadata and controls
99 lines (75 loc) · 2.13 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
SELECT
Class,
COUNT(*) AS transaction_count,
ROUND(
100.0 * COUNT(*) / (SELECT COUNT(*) FROM transactions),
4
) AS percentage
FROM transactions
GROUP BY Class;
-----------------------------------------------------------------
SELECT
Class,
ROUND(AVG(Amount), 2) AS avg_amount
FROM transactions
GROUP BY Class;
-----------------------------------------------------------------
SELECT
CAST((Time / 3600) % 24 AS INTEGER) AS hour_of_day,
COUNT(*) AS total_transactions,
SUM(Class) AS fraud_transactions,
ROUND(1.0 * SUM(Class) / COUNT(*), 5) AS fraud_rate
FROM transactions
GROUP BY hour_of_day
ORDER BY hour_of_day;
-----------------------------------------------------------------
SELECT
Time,
Class,
ROUND(
AVG(Class) OVER (
ORDER BY Time
ROWS BETWEEN 1000 PRECEDING AND CURRENT ROW
),
5
) AS rolling_fraud_rate
FROM transactions
ORDER BY Time
LIMIT 5000;
------------------------------------------------------------------
SELECT
CASE
WHEN Amount < 10 THEN 'Low (<10)'
WHEN Amount BETWEEN 10 AND 100 THEN 'Medium (10-100)'
ELSE 'High (>100)'
END AS amount_bucket,
COUNT(*) AS total_transactions,
SUM(Class) AS fraud_transactions,
ROUND(1.0 * SUM(Class) / COUNT(*), 5) AS fraud_rate
FROM transactions
GROUP BY amount_bucket
ORDER BY fraud_rate DESC;
-------------------------------------------------------------------
SELECT
CAST((Time / 3600) % 24 AS INTEGER) AS hour_of_day,
COUNT(*) AS fraud_transactions
FROM transactions
WHERE Class = 1
GROUP BY hour_of_day
ORDER BY fraud_transactions DESC
LIMIT 5;
-------------------------------------------------------------------
SELECT
Time,
Amount,
Class,
AVG(Amount) OVER (
ORDER BY Time
ROWS BETWEEN 500 PRECEDING AND CURRENT ROW
) AS rolling_avg_amount,
SUM(Class) OVER (
ORDER BY Time
ROWS BETWEEN 500 PRECEDING AND CURRENT ROW
) AS rolling_fraud_count
FROM transactions
LIMIT 3000;