Last active
March 31, 2021 23:25
-
-
Save garystafford/a086c4ca703c7da52828e7cf7a4cd0ec 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
-- temperature and humidity | |
CREATE VIEW temperature_humidity_summary_minute WITH (timescaledb.continuous) | |
AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time) AS bucket, | |
AVG(temperature) AS avg_temp, | |
AVG(humidity) AS avg_humidity | |
FROM sensor_data | |
GROUP BY device_id, | |
bucket; | |
-- air quality (lpg, co, smoke) | |
CREATE VIEW air_quality_summary_minute WITH (timescaledb.continuous) | |
AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time) AS bucket, | |
AVG(lpg) AS avg_lpg, | |
AVG(co) AS avg_co, | |
AVG(smoke) AS avg_smoke | |
FROM sensor_data | |
GROUP BY device_id, | |
bucket; | |
-- light | |
CREATE VIEW light_summary_minute WITH (timescaledb.continuous) | |
AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time) AS bucket, | |
AVG( | |
case | |
when light = 't' then 1 | |
else 0 | |
end | |
) AS avg_light | |
FROM sensor_data | |
GROUP BY device_id, | |
bucket; | |
-- motion | |
CREATE VIEW motion_summary_minute WITH (timescaledb.continuous) | |
AS | |
SELECT device_id, | |
time_bucket(INTERVAL '1 minute', time) AS bucket, | |
AVG( | |
case | |
when motion = 't' then 1 | |
else 0 | |
end | |
) AS avg_motion | |
FROM sensor_data | |
GROUP BY device_id, | |
bucket; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment