adjusted select

This commit is contained in:
Dona Maria Absi
2025-05-15 12:28:38 +03:00
parent b50d7682f3
commit 56e78683b3

View File

@ -4,23 +4,14 @@ WITH params AS (
TO_DATE(NULLIF($2, ''), 'YYYY-MM') AS event_month
)
, step1 AS (
SELECT
space_uuid,
TO_CHAR(event_date, 'YYYY-MM') AS event_month,
DATE_PART('day', (date_trunc('month', event_date) + INTERVAL '1 month - 1 day')) AS days_in_month,
SUM(occupied_seconds) AS occupied_seconds
FROM public."space-daily-occupancy-duration" AS sdod
GROUP BY space_uuid, event_month, days_in_month
)
SELECT
step1.space_uuid,
step1.event_month,
occupied_seconds / (days_in_month * 86400.0) * 100 AS occupancy_percentage
FROM step1
JOIN params P ON step1.space_uuid = P.space_uuid
WHERE P.event_month IS NULL
OR step1.event_month = TO_CHAR(P.event_month, 'YYYY-MM')
ORDER BY step1.space_uuid, step1.event_month;
SELECT sdo.space_uuid,
event_date,
occupancy_percentage,
occupied_seconds
FROM public."space-daily-occupancy-duration" as sdo
JOIN params P ON true
where (sdo.space_uuid = P.space_uuid
OR P.event_month IS null)
AND TO_CHAR(sdo.event_date, 'YYYY-MM') = TO_CHAR(P.event_month, 'YYYY-MM')
ORDER BY sdo.space_uuid, sdo.event_date;