mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 16:44:55 +00:00
adjusted select
This commit is contained in:
@ -4,23 +4,14 @@ WITH params AS (
|
|||||||
TO_DATE(NULLIF($2, ''), 'YYYY-MM') AS event_month
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user