-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2023_week_08.sql
More file actions
121 lines (95 loc) · 2.89 KB
/
2023_week_08.sql
File metadata and controls
121 lines (95 loc) · 2.89 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
USE DATABASE TIL_PLAYGROUND;
USE SCHEMA preppin_data_inputs;
--- preppin data week 8 ---
-- Input each of the 12 monthly files
-- Create a 'file date' using the month found in the file name
-- The Null value should be replaced as 1
-- Clean the Market Cap value to ensure it is the true value as 'Market Capitalisation'
-- Remove any rows with 'n/a'
-- Categorise the Purchase Price into groupings
-- 0 to 24,999.99 as 'Low'
-- 25,000 to 49,999.99 as 'Medium'
-- 50,000 to 74,999.99 as 'High'
-- 75,000 to 100,000 as 'Very High'
-- Categorise the Market Cap into groupings
-- Below $100M as 'Small'
-- Between $100M and below $1B as 'Medium'
-- Between $1B and below $100B as 'Large'
-- $100B and above as 'Huge'
-- Rank the highest 5 purchases per combination of: file date, Purchase Price Categorisation and Market Capitalisation Categorisation.
-- Output only records with a rank of 1 to 5
WITH CTE AS (
SELECT *, 'PD2023_WK08_01' file_name
FROM PD2023_WK08_01
UNION ALL
SELECT *, 'PD2023_WK08_02' file_name
FROM PD2023_WK08_02
UNION ALL
SELECT *, 'PD2023_WK08_03' file_name
FROM PD2023_WK08_03
UNION ALL
SELECT *, 'PD2023_WK08_04' file_name
FROM PD2023_WK08_04
UNION ALL
SELECT *, 'PD2023_WK08_05' file_name
FROM PD2023_WK08_05
UNION ALL
SELECT *, 'PD2023_WK08_06' file_name
FROM PD2023_WK08_06
UNION ALL
SELECT *, 'PD2023_WK08_07' file_name
FROM PD2023_WK08_07
UNION ALL
SELECT *, 'PD2023_WK08_08' file_name
FROM PD2023_WK08_08
UNION ALL
SELECT *, 'PD2023_WK08_09' file_name
FROM PD2023_WK08_09
UNION ALL
SELECT *, 'PD2023_WK08_10' file_name
FROM PD2023_WK08_10
UNION ALL
SELECT *, 'PD2023_WK08_11' file_name
FROM PD2023_WK08_11
UNION ALL
SELECT *, 'PD2023_WK08_12' file_name
FROM PD2023_WK08_12
)
, OUTPUT AS (
SELECT *,
DATE_FROM_PARTS('2023', SPLIT_PART(file_name, '_', 3), '01') file_date,
CASE
WHEN market_cap LIKE '%M' THEN ROUND((REPLACE(REPLACE(market_cap, '$', ''), 'M', '')*1000000), 2)
WHEN market_cap LIKE '%B' THEN ROUND((REPLACE(REPLACE(market_cap, '$', ''), 'B', '')*1000000000),2)
ELSE ROUND(REPLACE(market_cap, '$', ''),2)
END as market_capitalisation,
REPLACE(purchase_price, '$', ''):: int purchase_price2,
CASE
WHEN purchase_price2 <=24999.99 THEN 'Low'
WHEN purchase_price2 <=49999.99 THEN 'Medium'
WHEN purchase_price2 <=74999.99 THEN 'High'
WHEN purchase_price2 <=100000 THEN 'Very High'
END as purchase_group,
CASE
WHEN market_capitalisation < 100000000 THEN 'Samll'
WHEN market_capitalisation < 1000000000 THEN 'Medium'
WHEN market_capitalisation < 100000000000 THEN 'Large'
ELSE 'Huge'
END as market_group ,
RANK() OVER (PARTITION BY file_date, purchase_group, market_group ORDER BY REPLACE(purchase_price, '$', ''):: int DESC) rnk
FROM CTE
WHERE market_cap <> 'n/a'
)
SELECT
market_group,
purchase_group,
file_date,
ticker,
sector,
market,
stock_name,
market_cap,
purchase_price,
rnk as rank
FROM OUTPUT
WHERE rnk < 6;