-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10-monthly-merchant-balance.sql
46 lines (33 loc) · 1.33 KB
/
10-monthly-merchant-balance.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
-- Monthly Merchant Balance [Visa SQL Interview Question]
-- Say you have access to all the transactions for a given merchant account.
-- Write a query to print the cumulative balance of the merchant account at the end of each day, with the total balance reset back to zero at the end of the month. Output the transaction date and cumulative balance.
-- transactions Table:
-- Column Name Type
-- transaction_id integer
-- type string ('deposit', 'withdrawal')
-- amount decimal
-- transaction_date timestamp
-- transactions Example Input:
-- transaction_id type amount transaction_date
-- 19153 deposit 65.90 07/10/2022 10:00:00
-- 53151 deposit 178.55 07/08/2022 10:00:00
-- 29776 withdrawal 25.90 07/08/2022 10:00:00
-- 16461 withdrawal 45.99 07/08/2022 10:00:00
-- 77134 deposit 32.60 07/10/2022 10:00:00
-- Example Output:
-- transaction_date balance
-- 07/08/2022 12:00:00 106.66
-- 07/10/2022 12:00:00 205.16
WITH CTE AS (
SELECT transaction_id,
transaction_date,
EXTRACT(DAY FROM transaction_date) AS day,
EXTRACT(MONTH FROM transaction_date) AS month,
CASE WHEN type='deposit' THEN amount ELSE -amount END as revised_amount
FROM transactions
)
SELECT DISTINCT transaction_date,
SUM(revised_amount) OVER(PARTITION BY month
ORDER BY day) as balance
FROM CTE
ORDER BY 1