This commit is contained in:
Dona Maria Absi
2025-05-14 15:50:28 +03:00
parent 1bb3803229
commit b50d7682f3
2 changed files with 27 additions and 10 deletions

View File

@ -90,6 +90,7 @@ occupied_seconds_per_day AS (
, final_data as (
SELECT space_id,
event_date,
total_occupied_seconds,
occupancy_prct
FROM occupied_seconds_per_day
ORDER BY 1,2
@ -98,12 +99,17 @@ ORDER BY 1,2
INSERT INTO public."space-daily-occupancy-duration" (
space_uuid,
event_date,
occupied_seconds,
occupancy_percentage
)
select space_id,
event_date,
total_occupied_seconds,
occupancy_prct
FROM final_data
ON CONFLICT (space_uuid, event_date) DO UPDATE
SET
occupancy_percentage = EXCLUDED.occupancy_percentage;

View File

@ -1,15 +1,26 @@
WITH params AS (
SELECT
$1::uuid AS space_uuid,
TO_DATE(NULLIF($2, ''), 'YYYY-MM-DD') AS event_date
TO_DATE(NULLIF($2, ''), 'YYYY-MM') AS event_month
)
SELECT sdod.*
, 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
JOIN params P ON true
WHERE sdod.space_uuid = P.space_uuid
AND (
P.event_year IS NULL
OR sdod.event_date = P.event_year
GROUP BY space_uuid, event_month, days_in_month
)
ORDER BY space_uuid, event_date;
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;