mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
updates
This commit is contained in:
@ -90,6 +90,7 @@ occupied_seconds_per_day AS (
|
|||||||
, final_data as (
|
, final_data as (
|
||||||
SELECT space_id,
|
SELECT space_id,
|
||||||
event_date,
|
event_date,
|
||||||
|
total_occupied_seconds,
|
||||||
occupancy_prct
|
occupancy_prct
|
||||||
FROM occupied_seconds_per_day
|
FROM occupied_seconds_per_day
|
||||||
ORDER BY 1,2
|
ORDER BY 1,2
|
||||||
@ -98,12 +99,17 @@ ORDER BY 1,2
|
|||||||
INSERT INTO public."space-daily-occupancy-duration" (
|
INSERT INTO public."space-daily-occupancy-duration" (
|
||||||
space_uuid,
|
space_uuid,
|
||||||
event_date,
|
event_date,
|
||||||
|
occupied_seconds,
|
||||||
occupancy_percentage
|
occupancy_percentage
|
||||||
)
|
)
|
||||||
select space_id,
|
select space_id,
|
||||||
event_date,
|
event_date,
|
||||||
|
total_occupied_seconds,
|
||||||
occupancy_prct
|
occupancy_prct
|
||||||
FROM final_data
|
FROM final_data
|
||||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||||
SET
|
SET
|
||||||
occupancy_percentage = EXCLUDED.occupancy_percentage;
|
occupancy_percentage = EXCLUDED.occupancy_percentage;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,15 +1,26 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS space_uuid,
|
$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 (
|
||||||
FROM public."space-daily-occupancy-duration" AS sdod
|
SELECT
|
||||||
JOIN params P ON true
|
space_uuid,
|
||||||
WHERE sdod.space_uuid = P.space_uuid
|
TO_CHAR(event_date, 'YYYY-MM') AS event_month,
|
||||||
AND (
|
DATE_PART('day', (date_trunc('month', event_date) + INTERVAL '1 month - 1 day')) AS days_in_month,
|
||||||
P.event_year IS NULL
|
SUM(occupied_seconds) AS occupied_seconds
|
||||||
OR sdod.event_date = P.event_year
|
FROM public."space-daily-occupancy-duration" AS sdod
|
||||||
|
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;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user