diff --git a/fact_daily_device_presence_duration/fact_daily_device_presence_duration.sql b/fact_daily_device_presence_duration/fact_daily_device_presence_duration.sql index 46b0548..1bcec42 100644 --- a/fact_daily_device_presence_duration/fact_daily_device_presence_duration.sql +++ b/fact_daily_device_presence_duration/fact_daily_device_presence_duration.sql @@ -35,7 +35,7 @@ time_differences AS ( event_time::date AS event_date, EXTRACT(EPOCH FROM (event_time - COALESCE(prev_timestamp, event_time))) AS time_diff_in_seconds FROM start_date -) +), duration as ( diff --git a/fact_daily_space_presence_duration/fact_daily_space_presence_duration.sql b/fact_daily_space_presence_duration/fact_daily_space_presence_duration.sql index e0d2251..c6b93c5 100644 --- a/fact_daily_space_presence_duration/fact_daily_space_presence_duration.sql +++ b/fact_daily_space_presence_duration/fact_daily_space_presence_duration.sql @@ -95,4 +95,4 @@ LEFT JOIN daily_empty_duration d ON a.space_id = d.space_id AND a.occupancy_date = d.occupancy_date join "space" s - on s."uuid" =a.space_id; \ No newline at end of file + on s."uuid" =a.space_id \ No newline at end of file diff --git a/fact_daily_space_presence_duration/testing personal.sql b/fact_daily_space_presence_duration/testing personal.sql new file mode 100644 index 0000000..e8b6cd6 --- /dev/null +++ b/fact_daily_space_presence_duration/testing personal.sql @@ -0,0 +1,27 @@ +SELECT + device.uuid AS device_id, + device.space_device_uuid AS space_id, + -- Mark none_flag if value is 'none' + CASE + WHEN "device-status-log".value = 'none' THEN 1 + ELSE 0 + END AS none_flag, + "device-status-log".event_time::timestamp AS event_time, + LAG("device-status-log".event_time::timestamp) + OVER (PARTITION BY device.uuid + ORDER BY "device-status-log".event_time) AS prev_timestamp, + LAG( + CASE + WHEN "device-status-log".value = 'none' THEN 1 -- identifies if there was a 'none' flag detected + ELSE 0 + END + ) OVER (PARTITION BY device.uuid + ORDER BY "device-status-log".event_time) AS prev_none_flag + FROM device + LEFT JOIN "device-status-log" + ON device.uuid = "device-status-log".device_id + LEFT JOIN product + ON product.uuid = device.product_device_uuid + WHERE product.cat_name = 'hps' + AND "device-status-log".code = 'presence_state' + ORDER BY device.uuid, "device-status-log".event_time \ No newline at end of file diff --git a/fact_daily_subspace_presence_detected/fact_daily_subspace_presence_detected.sql b/fact_daily_subspace_presence_detected/fact_daily_subspace_presence_detected.sql index 980c923..ff5be34 100644 --- a/fact_daily_subspace_presence_detected/fact_daily_subspace_presence_detected.sql +++ b/fact_daily_subspace_presence_detected/fact_daily_subspace_presence_detected.sql @@ -18,7 +18,7 @@ WITH device_logs AS ( ORDER BY "device-status-log".event_time) AS prev_value FROM device LEFT JOIN "device-status-log" - ON device.uuid::text = "device-status-log".device_id + ON device.uuid = "device-status-log".device_id LEFT JOIN product ON product.uuid = device.product_device_uuid WHERE product.cat_name = 'hps' @@ -68,5 +68,5 @@ INNER JOIN subspace INNER JOIN "space" AS space ON space.uuid = device.space_device_uuid WHERE "device-status-log".code = 'presence_state' -GROUP BY 1, 2, 3, 4, 5,6 +GROUP BY 1, 2, 3, 4, 5,6; diff --git a/fact_power_clamp_daily_energy_consumed/fact_power_clamp_daily_energy_clamp.sql b/fact_power_clamp_daily_energy_consumed/fact_power_clamp_daily_energy_clamp.sql index e69de29..7499892 100644 --- a/fact_power_clamp_daily_energy_consumed/fact_power_clamp_daily_energy_clamp.sql +++ b/fact_power_clamp_daily_energy_consumed/fact_power_clamp_daily_energy_clamp.sql @@ -0,0 +1,9 @@ +SELECT distinct(code) +FROM device +LEFT JOIN "device-status-log" + ON device.uuid = "device-status-log".device_id +LEFT JOIN product + ON product.uuid = device.product_device_uuid +where product.name='Power Clamp' +--and code='VoltageA' +--ORDER BY device.uuid, "device-status-log".event_time \ No newline at end of file diff --git a/fact_thermostat_duration/fact_thermostat_duration.sql b/fact_thermostat_duration/fact_thermostat_duration.sql new file mode 100644 index 0000000..3a353fd --- /dev/null +++ b/fact_thermostat_duration/fact_thermostat_duration.sql @@ -0,0 +1,54 @@ +WITH avg_set_temp AS ( + -- Step 1: Compute the average set temperature per device per hour + SELECT + device.uuid AS device_id, + device.space_device_uuid AS space_id, + event_time::date AS date, + DATE_PART('hour', event_time) AS hour, + AVG("device-status-log".value::INTEGER) AS avg_set_temp + FROM device + LEFT JOIN "device-status-log" + ON device.uuid = "device-status-log".device_id + LEFT JOIN product + ON product.uuid = device.product_device_uuid + WHERE product.name = 'Smart Thermostat' + AND "device-status-log".code = 'temp_set' + 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 + d.uuid AS device_id, + d.space_device_uuid AS space_id, + l.event_time::date AS date, + DATE_PART('hour', l.event_time) AS hour, + CASE WHEN l.code = 'temp_current' THEN l.value END AS current_temp, + f.filled_avg_set_temp +FROM device d +LEFT JOIN "device-status-log" l + ON d.uuid = l.device_id +LEFT JOIN product p + ON p.uuid = d.product_device_uuid +LEFT JOIN avg_set_temp_filled f + ON f.device_id = d.uuid + AND f.date = l.event_time::date + and f.date = l.event_time::date AND f.hour <= DATE_PART('hour', l.event_time) +WHERE p.name = 'Smart Thermostat' +and l.code = 'temp_current' +ORDER BY d.uuid, l.event_time;