-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_create_views.sql
More file actions
127 lines (121 loc) · 3.85 KB
/
Copy path01_create_views.sql
File metadata and controls
127 lines (121 loc) · 3.85 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
122
123
124
125
126
127
-- Smart Power: MySQL 8 views used by the analysis and Tableau dashboard.
-- Source timestamps are UTC. Convert to Europe/Amsterdam in Tableau so DST
-- is handled correctly in the presentation layer.
USE smart_power;
DROP VIEW IF EXISTS vw_hourly_summary_utc;
DROP VIEW IF EXISTS vw_daily_summary_utc;
DROP VIEW IF EXISTS vw_hourly_dashboard;
DROP VIEW IF EXISTS vw_analysis_thresholds;
-- Match pandas' linearly interpolated quartiles:
-- Bottom 25% of price = cheap; bottom 25% of carbon intensity = low-carbon.
CREATE VIEW vw_analysis_thresholds AS
WITH
stats AS (
SELECT COUNT(*) AS n
FROM hourly_data
),
positions AS (
SELECT
n,
(n - 1) * 0.25 AS price_index,
(n - 1) * 0.25 AS carbon_index
FROM stats
),
price_ranked AS (
SELECT
electricity_price AS value,
ROW_NUMBER() OVER (ORDER BY electricity_price) AS rn
FROM hourly_data
),
carbon_ranked AS (
SELECT
carbon_intensity_gco2_kwh AS value,
ROW_NUMBER() OVER (ORDER BY carbon_intensity_gco2_kwh) AS rn
FROM hourly_data
)
SELECT
(
SELECT value
FROM price_ranked
WHERE rn = FLOOR(positions.price_index) + 1
) +
(positions.price_index - FLOOR(positions.price_index)) *
(
(
SELECT value
FROM price_ranked
WHERE rn = LEAST(FLOOR(positions.price_index) + 2, positions.n)
) -
(
SELECT value
FROM price_ranked
WHERE rn = FLOOR(positions.price_index) + 1
)
) AS cheap_price_threshold,
(
SELECT value
FROM carbon_ranked
WHERE rn = FLOOR(positions.carbon_index) + 1
) +
(positions.carbon_index - FLOOR(positions.carbon_index)) *
(
(
SELECT value
FROM carbon_ranked
WHERE rn = LEAST(FLOOR(positions.carbon_index) + 2, positions.n)
) -
(
SELECT value
FROM carbon_ranked
WHERE rn = FLOOR(positions.carbon_index) + 1
)
) AS low_carbon_threshold_gco2_kwh
FROM positions;
CREATE VIEW vw_hourly_dashboard AS
SELECT
h.timestamp_utc,
DATE(h.timestamp_utc) AS date_utc,
HOUR(h.timestamp_utc) AS hour_utc,
DAYOFWEEK(h.timestamp_utc) - 1 AS weekday_number_utc,
DAYNAME(h.timestamp_utc) AS weekday_name_utc,
h.electricity_price,
h.renewable_share,
h.renewable_generation,
h.total_generation,
h.carbon_intensity_gco2_kwh,
h.wind_speed,
h.solar_radiation,
CASE WHEN h.electricity_price <= t.cheap_price_threshold THEN 1 ELSE 0 END AS is_cheap,
CASE WHEN h.carbon_intensity_gco2_kwh <= t.low_carbon_threshold_gco2_kwh THEN 1 ELSE 0 END AS is_low_carbon,
CASE
WHEN h.electricity_price <= t.cheap_price_threshold
AND h.carbon_intensity_gco2_kwh <= t.low_carbon_threshold_gco2_kwh
THEN 1 ELSE 0
END AS is_cheap_and_low_carbon
FROM hourly_data AS h
CROSS JOIN vw_analysis_thresholds AS t;
CREATE VIEW vw_daily_summary_utc AS
SELECT
date_utc,
COUNT(*) AS observed_hours,
AVG(electricity_price) AS avg_price,
MIN(electricity_price) AS min_price,
MAX(electricity_price) AS max_price,
AVG(renewable_share) AS avg_renewable_share,
AVG(carbon_intensity_gco2_kwh) AS avg_carbon_intensity_gco2_kwh,
AVG(wind_speed) AS avg_wind_speed,
AVG(solar_radiation) AS avg_solar_radiation,
SUM(is_cheap_and_low_carbon) AS cheap_low_carbon_hours
FROM vw_hourly_dashboard
GROUP BY date_utc;
CREATE VIEW vw_hourly_summary_utc AS
SELECT
hour_utc,
COUNT(*) AS observed_hours,
AVG(electricity_price) AS avg_price,
AVG(renewable_share) AS avg_renewable_share,
AVG(carbon_intensity_gco2_kwh) AS avg_carbon_intensity_gco2_kwh,
SUM(is_cheap_and_low_carbon) AS cheap_low_carbon_hours,
1.0 * SUM(is_cheap_and_low_carbon) / COUNT(*) AS cheap_low_carbon_rate
FROM vw_hourly_dashboard
GROUP BY hour_utc;