-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_TRANSACTION_INSIGHTS.sql
More file actions
161 lines (130 loc) · 3.79 KB
/
SQL_TRANSACTION_INSIGHTS.sql
File metadata and controls
161 lines (130 loc) · 3.79 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Use credit_debit;
ALTER TABLE credit_debit_data
RENAME COLUMN `Account Number` TO `Account_Number`;
-- Question 1
SELECT
ROUND(SUM(Amount), 2) AS Total_Credit_Amount
FROM `credit_debit_data`
WHERE `Transaction Type` = 'Credit';
-- Question 2
SELECT
ROUND(SUM(Amount), 2) AS Total_Credit_Amount
FROM `credit_debit_data`
WHERE `Transaction Type` = 'Debit';
-- Question 3
SELECT
SUM(CASE WHEN `Transaction Type` = 'Credit' THEN Amount ELSE 0 END) /
NULLIF(SUM(CASE WHEN `Transaction Type` = 'Debit' THEN Amount ELSE 0 END), 0) AS Credit_to_Debit_Ratio
FROM `credit_debit_data`;
-- Question 4
SELECT
SUM(CASE
WHEN `Transaction Type` = 'Credit' THEN Amount
WHEN `Transaction Type` = 'Debit' THEN -Amount
ELSE 0
END) AS Net_Transaction_Amount
FROM `credit_debit_data`;
-- Question 5.Account Activity Ratio
SELECT
Account_Number,
COUNT(*) AS Number_of_Transactions,
MAX(Balance) AS Account_Balance,
ROUND(COUNT(*) / MAX(Balance), 4) AS Account_Activity_Ratio
FROM credit_debit_data
GROUP BY Account_Number;
-- Question 6.a Transactions per Day
SELECT
DATE(`Transaction Date`) AS Transaction_Date,
COUNT(*) AS Transactions_Per_Day
FROM credit_debit_data
GROUP BY DATE(`Transaction Date`)
ORDER BY Transaction_Date;
-- Question 6.b Transactions per Week
SELECT
YEAR(`Transaction Date`) AS Year,
WEEK(`Transaction Date`) AS Week_Number,
COUNT(*) AS Transactions_Per_Week
FROM credit_debit_data
GROUP BY YEAR(`Transaction Date`), WEEK(`Transaction Date`)
ORDER BY Year, Week_Number;
-- Question 6.c Transactions per Month
SELECT
YEAR(`Transaction Date`) AS Year,
MONTH(`Transaction Date`) AS Month,
COUNT(*) AS Transactions_Per_Month
FROM credit_debit_data
GROUP BY YEAR(`Transaction Date`), MONTH(`Transaction Date`)
ORDER BY Year, Month;
-- Question 7. Total Transaction Amount by Branch
SELECT
Branch,
ROUND(SUM(Amount), 2) AS Total_Transaction_Amount
FROM credit_debit_data
GROUP BY Branch
ORDER BY Total_Transaction_Amount DESC;
-- Question 8. Transaction Volume by Bank
SELECT
`Bank Name`,
ROUND(SUM(Amount), 2) AS Total_Transaction_Amount
FROM credit_debit_data
GROUP BY `Bank Name`
ORDER BY Total_Transaction_Amount DESC;
-- Question 9
SELECT
t.`Transaction Method`,
t.cnt AS transaction_count,
ROUND(100 * t.cnt / NULLIF(s.total_count, 0), 2) AS pct_of_total
FROM (
SELECT `Transaction Method`, COUNT(*) AS cnt
FROM `credit_debit_data`
GROUP BY `Transaction Method`
) AS t
CROSS JOIN (
SELECT COUNT(*) AS total_count
FROM `credit_debit_data`
) AS s
ORDER BY t.cnt DESC;
-- Question 10
SELECT
Branch,
month,
total_amount,
prev_amount,
ROUND(100 * (total_amount - prev_amount) / NULLIF(prev_amount, 0), 2) AS pct_change_amount
FROM (
SELECT
Branch,
DATE_FORMAT(`Transaction Date`, '%Y-%m') AS month,
ROUND(SUM(Amount), 2) AS total_amount,
LAG(ROUND(SUM(Amount),2)) OVER (PARTITION BY Branch ORDER BY DATE_FORMAT(`Transaction Date`, '%Y-%m')) AS prev_amount
FROM `credit_debit_data`
GROUP BY Branch, month
) t
ORDER BY Branch, month;
-- Question 11
SET @THRESHOLD := 4800;
SELECT
Risk_Flag AS 'Row Labels',
COUNT(`Customer ID`) AS 'Count of Customer ID'
FROM (
SELECT
`Customer ID`,
CASE
WHEN Amount >= @THRESHOLD
OR Amount > 3 * AVG(Amount) OVER (PARTITION BY `Account Number`)
THEN 'Risky'
ELSE 'Normal'
END AS Risk_Flag
FROM `credit_debit_data`
) AS sub
GROUP BY Risk_Flag WITH ROLLUP;
-- Question 12
SELECT
DATE_FORMAT(`Transaction Date`, '%Y-%m') AS Month,
COUNT(*) AS Suspicious_Transaction_Frequency
FROM credit_debit_data
WHERE
Amount > 4999.9
OR Description IN ('Bonus Payment', 'Refund for Overcharge', 'Freelance Payment')
GROUP BY DATE_FORMAT(`Transaction Date`, '%Y-%m')
order by month;