input year, output month

This commit is contained in:
Dona Maria Absi
2025-05-22 13:10:05 +03:00
parent f2e515b180
commit fe891030aa

View File

@ -0,0 +1,21 @@
WITH params AS (
SELECT
TO_DATE(NULLIF($2, ''), 'YYYY') AS year,
string_to_array(NULLIF($4, ''), ',') AS device_ids
)
SELECT
A.device_uuid,
TO_CHAR(date_trunc('month', A.event_date), 'YYYY-MM') AS event_month,
SUM(A.count_motion_detected) AS total_motion_detected,
SUM(A.count_presence_detected) AS total_presence_detected,
SUM(A.count_total_presence_detected) AS total_overall_presence
FROM public."presence-sensor-daily-device-detection" AS A
JOIN params P ON TRUE
WHERE A.device_uuid::text = ANY(P.device_ids)
AND (
P.year IS NULL
OR date_trunc('year', A.event_date) = P.year
)
GROUP BY 1,2
ORDER BY 1,2;