space_energy_consumed_procedure

This commit is contained in:
Dona Maria Absi
2025-05-01 12:21:40 +03:00
parent 25403d5e19
commit ccba81ef1f
7 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,22 @@
WITH params AS (
SELECT
NULLIF($1, '')::uuid AS space_id,
NULLIF($2, '')::text AS start_month, -- Format: 'MM-YYYY'
NULLIF($3, '')::text AS end_month
)
SELECT
B.space_device_uuid AS space_id,
A.month,
SUM(A.energy_consumed_kW::numeric) AS total_energy_consumed_KW,
SUM(A.energy_consumed_A::numeric) AS total_energy_consumed_A,
SUM(A.energy_consumed_B::numeric) AS total_energy_consumed_B,
SUM(A.energy_consumed_C::numeric) AS total_energy_consumed_C
FROM public."power-clamp-energy-consumed-monthly" AS A
JOIN public.device AS B
ON A.device_uuid::TEXT = B."uuid"::TEXT
JOIN params P ON TRUE
WHERE B.space_device_uuid = P.space_id
AND (P.start_month IS NULL OR TO_DATE(A.month, 'MM-YYYY') >= TO_DATE(P.start_month, 'MM-YYYY'))
AND (P.end_month IS NULL OR TO_DATE(A.month, 'MM-YYYY') <= TO_DATE(P.end_month, 'MM-YYYY'))
GROUP BY 1, 2;

View File

@ -0,0 +1,11 @@
SELECT
B.space_device_uuid AS space_id,
A."date",
SUM(A.energy_consumed_kW::numeric) AS total_energy_consumed_KW,
SUM(A.energy_consumed_A::numeric) AS total_energy_consumed_A,
SUM(A.energy_consumed_B::numeric) AS total_energy_consumed_B,
SUM(A.energy_consumed_C::numeric) AS total_energy_consumed_C
FROM public."power-clamp-energy-consumed-daily" AS A -- I want to change the source table in the future.
JOIN public.device AS B
ON A.device_uuid::TEXT = B."uuid"::TEXT
GROUP BY 1, 2;