mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-07-10 07:07:18 +00:00
energy consumed
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
WITH first_last_times AS (
|
||||
WITH total_energy AS (
|
||||
-- Get the first and last event_time per device per day
|
||||
SELECT
|
||||
device_id,
|
||||
@ -10,9 +10,58 @@ WITH first_last_times AS (
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
SELECT
|
||||
device_id,
|
||||
date,
|
||||
(max_value-min_value) as energy_consumed_kW
|
||||
FROM first_last_times
|
||||
, energy_phase_A AS (
|
||||
-- Get the first and last event_time per device per day
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedA'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
, energy_phase_B AS (
|
||||
-- Get the first and last event_time per device per day
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedB'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
, energy_phase_C AS (
|
||||
-- Get the first and last event_time per device per day
|
||||
SELECT
|
||||
device_id,
|
||||
event_time::date AS date,
|
||||
MIN(value)::integer AS min_value,
|
||||
MAX(value)::integer AS max_value
|
||||
FROM "device-status-log"
|
||||
where code='EnergyConsumedC'
|
||||
GROUP BY device_id, date
|
||||
)
|
||||
|
||||
|
||||
SELECT
|
||||
total_energy.device_id,
|
||||
total_energy.date,
|
||||
(total_energy.max_value-total_energy.min_value) as energy_consumed_kW,
|
||||
(energy_phase_A.max_value-energy_phase_A.min_value) as energy_consumed_A,
|
||||
(energy_phase_B.max_value-energy_phase_B.min_value) as energy_consumed_B,
|
||||
(energy_phase_C.max_value-energy_phase_C.min_value) as energy_consumed_C
|
||||
FROM total_energy
|
||||
JOIN energy_phase_A
|
||||
ON total_energy.device_id=energy_phase_A.device_id
|
||||
and total_energy.date=energy_phase_A.date
|
||||
JOIN energy_phase_B
|
||||
ON total_energy.device_id=energy_phase_B.device_id
|
||||
and total_energy.date=energy_phase_B.date
|
||||
JOIN energy_phase_C
|
||||
ON total_energy.device_id=energy_phase_C.device_id
|
||||
and total_energy.date=energy_phase_C.date
|
||||
|
||||
|
@ -51,23 +51,21 @@ WITH start_date AS (
|
||||
AND a.device_id <> b.device_id
|
||||
AND a.start_time < b.end_time
|
||||
AND b.start_time < a.end_time
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
, daily_total_occupancy AS (
|
||||
SELECT
|
||||
start_date.space_id,
|
||||
dim_date.daily_date,
|
||||
t.space_id,
|
||||
DATE(t.start_time) AS occupancy_date,
|
||||
86,400 - COALESCE(SUM(o.overlap_duration_seconds), 0) AS total_occupancy_seconds
|
||||
FROM dim_date
|
||||
FROM time_intervals t
|
||||
LEFT JOIN overlaping_none_presence o
|
||||
--ON t.space_id = o.space_id
|
||||
ON dim_date.daily_date = o.overlap_start::date
|
||||
--AND t.start_time < o.overlap_end
|
||||
-- AND o.overlap_start < t.end_time
|
||||
left join start_date
|
||||
on start_date.space_id = o.space_id
|
||||
GROUP BY 1,2
|
||||
ON t.space_id = o.space_id
|
||||
AND t.start_time < o.overlap_end
|
||||
AND o.overlap_start < t.end_time
|
||||
GROUP BY t.space_id, DATE(t.start_time)
|
||||
)
|
||||
|
||||
SELECT * FROM daily_total_occupancy
|
||||
SELECT * FROM daily_total_occupancy
|
||||
|
||||
|
||||
|
@ -11,4 +11,5 @@ left join "permission-type" pt
|
||||
left join device
|
||||
on device."uuid" =dup.device_uuid
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
ON product.uuid = device.product_device_uuid;
|
||||
|
Reference in New Issue
Block a user