-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2023_week_09.sql
More file actions
62 lines (54 loc) · 1.82 KB
/
2023_week_09.sql
File metadata and controls
62 lines (54 loc) · 1.82 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
USE DATABASE TIL_PLAYGROUND;
USE SCHEMA preppin_data_inputs;
--- preppin data week 9 ---
--Input the data
--For the Transaction Path table:
-- Make sure field naming convention matches the other tables
-- i.e. instead of Account_From it should be Account From
-- Filter out the cancelled transactions
-- Split the flow into incoming and outgoing transactions
-- Bring the data together with the Balance as of 31st Jan
-- Work out the order that transactions occur for each account
-- Hint: where multiple transactions happen on the same day, assume the highest value transactions happen first
-- Use a running sum to calculate the Balance for each account on each day (hint)
-- The Transaction Value should be null for 31st Jan, as this is the starting balance
-- Output the data
WITH CTE AS (
--incoming
SELECT
tp.transaction_id,
tp.account_to account_number,
td.transaction_date,
value
FROM PD2023_WK07_TRANSACTION_PATH tp
INNER JOIN PD2023_WK07_TRANSACTION_DETAIL td ON td.transaction_id = tp. transaction_id
WHERE cancelled_ <> 'Y'
UNION ALL
--outgoing
SELECT
tp.transaction_id,
tp.account_from account_number,
td.transaction_date,
(-1)*value value
FROM PD2023_WK07_TRANSACTION_PATH tp
INNER JOIN PD2023_WK07_TRANSACTION_DETAIL td ON td.transaction_id = tp. transaction_id
WHERE cancelled_ <> 'Y'
UNION ALL
--jan 31 balance
SELECT
NULL transaction_id,
account_number,
'2023-01-31' transaction_date,
NULL value
FROM PD2023_WK07_ACCOUNT_INFORMATION
WHERE balance_date = '2023-01-31'
)
SELECT
c.account_number,
c.transaction_date balance_date,
c.value,
SUM(COALESCE(value, 0)) OVER (PARTITION BY c.account_number ORDER BY c.transaction_date, c.value DESC) + ai.balance account_balance
FROM CTE c
INNER JOIN PD2023_WK07_ACCOUNT_INFORMATION ai ON ai.account_number = c.account_number
WHERE balance_date = '2023-01-31'
ORDER BY 1,2,3 DESC;