energy consumed

This commit is contained in:
Dona Maria Absi
2025-03-18 15:17:03 +03:00
parent c231b000ce
commit 992fe20d4f
3 changed files with 68 additions and 20 deletions

View File

@ -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