mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-08-25 04:22:28 +00:00
presence
This commit is contained in:
@ -39,7 +39,7 @@ WITH start_date AS (
|
|||||||
WHERE prev_none_flag = 1
|
WHERE prev_none_flag = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
, overlaping_none_presence AS (
|
, overlaping_none_presence AS ( --assuming only 2 devices
|
||||||
SELECT
|
SELECT
|
||||||
a.space_id,
|
a.space_id,
|
||||||
GREATEST(a.start_time, b.start_time) AS overlap_start,
|
GREATEST(a.start_time, b.start_time) AS overlap_start,
|
||||||
@ -53,19 +53,31 @@ WITH start_date AS (
|
|||||||
AND b.start_time < a.end_time
|
AND b.start_time < a.end_time
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
, expanded_overlapping_none_presence AS (
|
||||||
|
SELECT distinct
|
||||||
|
o.space_id,
|
||||||
|
gs.date AS missing_date,
|
||||||
|
CASE
|
||||||
|
WHEN DATE(o.overlap_start) <> DATE(o.overlap_end) THEN 86400
|
||||||
|
ELSE EXTRACT(EPOCH FROM (LEAST(o.overlap_end, gs.date + INTERVAL '1 day') - GREATEST(o.overlap_start, gs.date)))
|
||||||
|
END AS missing_seconds
|
||||||
|
FROM overlaping_none_presence o
|
||||||
|
CROSS JOIN LATERAL generate_series(DATE(o.overlap_start), DATE(o.overlap_end) - INTERVAL '1 day', '1 day') AS gs(date)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
, daily_total_occupancy AS (
|
, daily_total_occupancy AS (
|
||||||
SELECT
|
SELECT
|
||||||
t.space_id,
|
t.space_id,
|
||||||
DATE(t.start_time) AS occupancy_date,
|
DATE(t.start_time) AS occupancy_date,
|
||||||
86,400 - COALESCE(SUM(o.overlap_duration_seconds), 0) AS total_occupancy_seconds
|
GREATEST(0, LEAST(86400, SUM(EXTRACT(EPOCH FROM (t.end_time - t.start_time))) -
|
||||||
|
COALESCE(SUM(e.missing_seconds), 0))) AS total_presence_seconds
|
||||||
FROM time_intervals t
|
FROM time_intervals t
|
||||||
LEFT JOIN overlaping_none_presence o
|
LEFT JOIN expanded_overlapping_none_presence e
|
||||||
ON t.space_id = o.space_id
|
ON t.space_id = e.space_id
|
||||||
AND t.start_time < o.overlap_end
|
AND DATE(t.start_time) = e.missing_date
|
||||||
AND o.overlap_start < t.end_time
|
|
||||||
GROUP BY t.space_id, DATE(t.start_time)
|
GROUP BY t.space_id, DATE(t.start_time)
|
||||||
)
|
)
|
||||||
|
|
||||||
SELECT * FROM daily_total_occupancy
|
SELECT * FROM daily_total_occupancy;
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user