-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path14-bank-account-summary.sql
65 lines (45 loc) · 2.01 KB
/
14-bank-account-summary.sql
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
-- Codestudio Bank (CSB) helps its coders in making virtual payments.
-- Our bank records all transactions in the table Transaction, we want to find out the current balance of all users and check wheter they have breached their credit limit (If their current credit is less than 0).
-- Write an SQL query to report.
-- - user_name
-- - user_id
-- - credit, current balance after performing transactions.
-- - credit_limit_breached, check credit_limit ("Yes" or "No")
-- Return the result table in any order.
-- Solution: accepted solution
SELECT u.user_id,
u.user_name,
u.credit + COALESCE(SUM(CASE WHEN t.paid_by = u.user_id THEN -t.amount ELSE t.amount END), 0) AS credit,
CASE WHEN u.credit + COALESCE(SUM(CASE WHEN t.paid_by = u.user_id THEN -t.amount ELSE t.amount END), 0) < 0 THEN 'Yes' ELSE 'No' END AS credit_limit_breached
FROM Users u LEFT JOIN Transactions t ON u.user_id IN (t.paid_by, t.paid_to)
GROUP BY u.user_id, u.user_name, u.credit
ORDER BY 1
-- Solution: wrong solution but correct approach.
WITH cte AS (
SELECT paid_by, SUM(CASE WHEN paid_by = trans_id THEN -amount ELSE amount END) AS net_balance
FROM Transactions
GROUP BY 1
)
SELECT u.user_id,
u.user_name,
COALESCE(u.credit + cte.net_balance, u.credit) AS credit,
CASE
WHEN COALESCE(u.credit + cte.net_balance, u.credit) < 0 THEN 'Yes'
ELSE 'No'
END AS credit_limit_breached
FROM Users u LEFT JOIN cte ON u.user_id = cte.paid_by
ORDER BY 1
-- my initial approach: got stuck here.
with cte as (
select *,
CASE WHEN trans_id=paid_by THEN credit+amount
WHEN trans_id=paid_to THEN credit-amount
END as updated_balance
from Transactions
)
SELECT u.user_id,
u.user_name,
updated_balance as 'credit',
CASE WHEN credit < 0 THEN 'Yes' ELSE 'No' END as credit_limit_breached
FROM Users u JOIN Transactions t ON u.user_id=t.paid_by or u.user_id=t.paid_to
-- remarks: