-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
42 lines (39 loc) · 954 Bytes
/
schema.sql
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
CREATE TABLE IF NOT EXISTS octopus_30m_data (
fuel LowCardinality(String),
start DateTime,
inserted SimpleAggregateFunction(min, DateTime) DEFAULT now(),
consumption SimpleAggregateFunction(max, Float64)
) ENGINE = AggregatingMergeTree
PARTITION BY toYYYYMM(start)
ORDER BY (fuel, start)
PRIMARY KEY (fuel, start);
CREATE VIEW IF NOT EXISTS octopus_30m AS (
SELECT
fuel,
start,
start + INTERVAL '30' MINUTE AS end,
min(inserted) AS inserted,
max(consumption) AS consumption
FROM octopus_30m_data
GROUP BY fuel, start
ORDER BY fuel, start
);
CREATE VIEW IF NOT EXISTS octopus_electricity_30m AS (
SELECT
start,
end,
inserted,
consumption AS kwh
FROM octopus_30m
WHERE fuel = 'electricity'
);
CREATE VIEW IF NOT EXISTS octopus_gas_30m AS (
SELECT
start,
end,
inserted,
consumption AS cubic_metres,
cubic_metres * 10 AS approx_kwh
FROM octopus_30m
WHERE fuel = 'gas'
);