Last active
April 2, 2021 20:18
-
-
Save garystafford/467efb714c68a414686762f470646281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create materialized views (continuous aggregates) | |
-- temperature and humidity | |
CREATE MATERIALIZED VIEW temperature_humidity_summary_minute(device_id, bucket, avg_temp, avg_humidity) | |
WITH (timescaledb.continuous) AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time), | |
avg(temperature), | |
avg(humidity) | |
FROM sensor_data | |
WHERE humidity BETWEEN 0 AND 100 | |
GROUP BY time_bucket(INTERVAL '1 minute', time), device_id | |
WITH NO DATA; | |
-- air quality (lpg, co, smoke) | |
CREATE MATERIALIZED VIEW air_quality_summary_minute(device_id, bucket, avg_lpg, avg_co, avg_smoke) | |
WITH (timescaledb.continuous) AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time), | |
avg(lpg), | |
avg(co), | |
avg(smoke) | |
FROM sensor_data | |
GROUP BY time_bucket(INTERVAL '1 minute', time), device_id | |
WITH NO DATA; | |
-- light | |
CREATE MATERIALIZED VIEW light_summary_minute(device_id, bucket, avg_light) | |
WITH (timescaledb.continuous) AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time), | |
avg(case when light = 't' then 1 else 0 end) | |
FROM sensor_data | |
GROUP BY time_bucket(INTERVAL '1 minute', time), device_id | |
WITH NO DATA; | |
-- motion | |
CREATE MATERIALIZED VIEW motion_summary_minute(device_id, bucket, avg_motion) | |
WITH (timescaledb.continuous) AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time), | |
avg(case when motion = 't' then 1 else 0 end) | |
FROM sensor_data | |
GROUP BY time_bucket(INTERVAL '1 minute', time), device_id | |
WITH NO DATA; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment