mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-11-26 07:24:55 +00:00
thermostat
This commit is contained in:
@ -14,41 +14,31 @@ WITH avg_set_temp AS (
|
|||||||
WHERE product.name = 'Smart Thermostat'
|
WHERE product.name = 'Smart Thermostat'
|
||||||
AND "device-status-log".code = 'temp_set'
|
AND "device-status-log".code = 'temp_set'
|
||||||
GROUP BY 1,2,3,4
|
GROUP BY 1,2,3,4
|
||||||
),
|
|
||||||
|
|
||||||
avg_set_temp_filled AS (
|
|
||||||
SELECT
|
|
||||||
device_id,
|
|
||||||
space_id,
|
|
||||||
date,
|
|
||||||
hour,
|
|
||||||
avg_set_temp,
|
|
||||||
COALESCE(
|
|
||||||
avg_set_temp,
|
|
||||||
LAG(avg_set_temp) OVER (
|
|
||||||
PARTITION BY device_id
|
|
||||||
ORDER BY date, hour
|
|
||||||
)
|
|
||||||
) AS filled_avg_set_temp
|
|
||||||
FROM avg_set_temp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SELECT
|
SELECT DISTINCT
|
||||||
d.uuid AS device_id,
|
d.uuid AS device_id,
|
||||||
d.space_device_uuid AS space_id,
|
d.space_device_uuid AS space_id,
|
||||||
l.event_time::date AS date,
|
l.event_time::date AS date,
|
||||||
|
-- l.event_time, -- Include event_time in SELECT to use it in ORDER BY
|
||||||
DATE_PART('hour', l.event_time) AS hour,
|
DATE_PART('hour', l.event_time) AS hour,
|
||||||
CASE WHEN l.code = 'temp_current' THEN l.value END AS current_temp,
|
CASE WHEN l.code = 'temp_current' THEN l.value END AS current_temp,
|
||||||
f.filled_avg_set_temp
|
COALESCE(f.avg_set_temp,
|
||||||
|
MAX(f.avg_set_temp) OVER (
|
||||||
|
PARTITION BY d.uuid
|
||||||
|
ORDER BY l.event_time
|
||||||
|
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
|
||||||
|
)
|
||||||
|
) AS final_filled_avg_set_temp
|
||||||
FROM device d
|
FROM device d
|
||||||
LEFT JOIN "device-status-log" l
|
LEFT JOIN "device-status-log" l
|
||||||
ON d.uuid = l.device_id
|
ON d.uuid = l.device_id
|
||||||
LEFT JOIN product p
|
LEFT JOIN product p
|
||||||
ON p.uuid = d.product_device_uuid
|
ON p.uuid = d.product_device_uuid
|
||||||
LEFT JOIN avg_set_temp_filled f
|
LEFT JOIN avg_set_temp f
|
||||||
ON f.device_id = d.uuid
|
ON f.device_id = d.uuid
|
||||||
AND f.date = l.event_time::date
|
AND f.date = l.event_time::date
|
||||||
and f.date = l.event_time::date AND f.hour <= DATE_PART('hour', l.event_time)
|
AND f.hour = DATE_PART('hour', l.event_time) -- Ensure correct hour match
|
||||||
WHERE p.name = 'Smart Thermostat'
|
WHERE p.name = 'Smart Thermostat'
|
||||||
and l.code = 'temp_current'
|
AND l.code = 'temp_current'
|
||||||
ORDER BY d.uuid, l.event_time;
|
ORDER BY 1,3;
|
||||||
|
|||||||
Reference in New Issue
Block a user