-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmarket_economic_analysis.sql
More file actions
111 lines (101 loc) · 3 KB
/
Copy pathmarket_economic_analysis.sql
File metadata and controls
111 lines (101 loc) · 3 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
{{
config(
description='Minimal test version - joining market returns with economic indicators'
)
}}
WITH economic_data AS (
SELECT
year_month,
series_code,
series_name,
avg_value AS economic_value,
pct_change_period AS economic_change_pct,
data_source AS economic_data_source,
-- Create month_date for joining
CASE
WHEN REGEXP_CONTAINS(year_month, '^\\d{4}-\\d{1,2}$')
THEN
DATE(
CAST(SPLIT(year_month, '-')[OFFSET(0)] AS INT64),
CAST(SPLIT(year_month, '-')[OFFSET(1)] AS INT64),
1
)
END AS month_date
FROM {{ ref('fred_quarterly_roc') }}
),
economic_indicators_pivoted AS (
SELECT
year_month,
month_date,
-- GDP indicators
MAX(
CASE
WHEN
series_code LIKE '%GDP%'
OR LOWER(series_name) LIKE '%gross domestic product%'
THEN economic_value
END
) AS gdp_value,
MAX(
CASE
WHEN
series_code LIKE '%GDP%'
OR LOWER(series_name) LIKE '%gross domestic product%'
THEN economic_change_pct
END
) AS gdp_change_pct,
-- Inflation indicators (CPI)
MAX(
CASE
WHEN
series_code LIKE '%CPI%'
OR LOWER(series_name) LIKE '%consumer price%'
THEN economic_value
END
) AS cpi_value,
MAX(
CASE
WHEN
series_code LIKE '%CPI%'
OR LOWER(series_name) LIKE '%consumer price%'
THEN economic_change_pct
END
) AS cpi_change_pct,
-- Interest rate indicators
MAX(
CASE
WHEN
LOWER(series_name) LIKE '%interest%' OR LOWER(series_name) LIKE '%rate%'
THEN economic_value
END
) AS interest_rate_value,
MAX(
CASE
WHEN
LOWER(series_name) LIKE '%interest%' OR LOWER(series_name) LIKE '%rate%'
THEN economic_change_pct
END
) AS interest_rate_change_pct
FROM economic_data
WHERE month_date IS NOT NULL
GROUP BY year_month, month_date
)
-- Start with just the economic data to test
SELECT
year_month,
month_date,
gdp_value,
gdp_change_pct,
cpi_value,
cpi_change_pct,
interest_rate_value,
interest_rate_change_pct,
-- Economic regime classification
CASE
WHEN cpi_change_pct > 2 THEN 'HIGH_INFLATION'
WHEN cpi_change_pct BETWEEN 0 AND 2 THEN 'MODERATE_INFLATION'
WHEN cpi_change_pct < 0 THEN 'DEFLATION'
ELSE 'UNKNOWN'
END AS inflation_regime
FROM economic_indicators_pivoted
ORDER BY month_date