/* Based on device data, this query returns the average * of the battery level for every hour for each device_id */ WITH avg_metrics AS ( SELECT device_id, DATE_BIN('1 hour'::INTERVAL, time, 0) AS period, AVG(battery_level) AS avg_battery_level FROM devices.readings GROUP BY 1, 2 ORDER BY 1, 2 ) SELECT period, t.device_id, manufacturer, avg_battery_level FROM avg_metrics t, devices.info i WHERE t.