-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2023_week_07.sql
More file actions
54 lines (49 loc) · 1.59 KB
/
2023_week_07.sql
File metadata and controls
54 lines (49 loc) · 1.59 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
USE DATABASE TIL_PLAYGROUND;
USE SCHEMA preppin_data_inputs;
--- preppin data week 7 ---
-- 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
-- For the Account Information table:
-- Make sure there are no null values in the Account Holder ID
-- Ensure there is one row per Account Holder ID
-- Joint accounts will have 2 Account Holders, we want a row for each of them
-- For the Account Holders table:
-- Make sure the phone numbers start with 07
-- Bring the tables together
-- Filter out cancelled transactions
-- Filter to transactions greater than £1,000 in value
-- Filter out Platinum accounts
-- Output the data
WITH ACCOUNT_INFORMATION AS (
SELECT
account_number,
account_type,
value as account_holder_id,
balance_date,
seq,
index
FROM PD2023_WK07_ACCOUNT_INFORMATION, LATERAL SPLIT_TO_TABLE(account_holder_id, ', ')
WHERE account_holder_id IS NOT NULL
)
SELECT
tp.transaction_id,
tp.account_to,
td.transaction_date,
td.value,
ai.account_number,
ai.account_type,
ai.balance_date,
ah.name,
ah.date_of_birth,
'0' || ah.contact_number as contact_number,
ah.first_line_of_address
FROM PD2023_WK07_ACCOUNT_HOLDERS as ah
INNER JOIN ACCOUNT_INFORMATION as ai ON ai.account_holder_id = ah.account_holder_id
INNER JOIN PD2023_WK07_TRANSACTION_PATH as tp ON tp.account_from = ai.account_number
INNER JOIN PD2023_WK07_TRANSACTION_DETAIL as td ON td.transaction_id = tp.transaction_id
WHERE
cancelled_ <> 'Y'
AND value > 1000
AND account_type <> 'Platinum';