mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-07-10 07:07:18 +00:00
update
This commit is contained in:
@ -39,34 +39,46 @@ WITH start_date AS (
|
|||||||
WHERE prev_none_flag = 1
|
WHERE prev_none_flag = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
, overlaping_none_presence AS ( --assuming only 2 devices
|
, overlapping_none_presence AS (
|
||||||
SELECT
|
SELECT -- spaces with 2 devices
|
||||||
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,
|
||||||
LEAST(a.end_time, b.end_time) AS overlap_end,
|
LEAST(a.end_time, b.end_time) AS overlap_end,
|
||||||
EXTRACT(EPOCH FROM (LEAST(a.end_time, b.end_time) - GREATEST(a.start_time, b.start_time))) AS overlap_duration_seconds
|
EXTRACT(EPOCH FROM (LEAST(a.end_time, b.end_time) - GREATEST(a.start_time, b.start_time))) AS overlap_duration_seconds
|
||||||
FROM time_intervals a
|
FROM time_intervals a
|
||||||
JOIN time_intervals b
|
LEFT JOIN time_intervals b
|
||||||
ON a.space_id = b.space_id
|
ON a.space_id = b.space_id
|
||||||
AND a.device_id <> b.device_id
|
AND a.device_id <> b.device_id
|
||||||
AND a.start_time < b.end_time
|
AND a.start_time < b.end_time
|
||||||
AND b.start_time < a.end_time
|
AND b.start_time < a.end_time
|
||||||
|
|
||||||
|
UNION
|
||||||
|
select --spaces with 1 device
|
||||||
|
a.space_id,
|
||||||
|
a.start_time AS overlap_start,
|
||||||
|
a.end_time AS overlap_end,
|
||||||
|
EXTRACT(EPOCH FROM (a.end_time - a.start_time)) AS overlap_duration_seconds
|
||||||
|
FROM time_intervals a
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM time_intervals b
|
||||||
|
WHERE a.space_id = b.space_id
|
||||||
|
AND a.device_id <> b.device_id
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
, expanded_overlapping_none_presence AS (
|
, expanded_overlapping_none_presence AS (
|
||||||
SELECT distinct
|
SELECT distinct
|
||||||
o.space_id,
|
o.space_id,
|
||||||
gs.date AS missing_date,
|
gs.date AS missing_date,
|
||||||
CASE
|
CASE
|
||||||
WHEN DATE(o.overlap_start) <> DATE(o.overlap_end) THEN 86400
|
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)))
|
ELSE EXTRACT(EPOCH FROM (LEAST(o.overlap_end, gs.date + INTERVAL '1 day') - GREATEST(o.overlap_start, gs.date)))
|
||||||
END AS missing_seconds
|
END AS missing_seconds
|
||||||
FROM overlaping_none_presence o
|
FROM overlapping_none_presence o
|
||||||
CROSS JOIN LATERAL generate_series(DATE(o.overlap_start), DATE(o.overlap_end) - INTERVAL '1 day', '1 day') AS gs(date)
|
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,
|
||||||
@ -79,8 +91,7 @@ WITH start_date AS (
|
|||||||
LEFT JOIN expanded_overlapping_none_presence e
|
LEFT JOIN expanded_overlapping_none_presence e
|
||||||
ON t.space_id = e.space_id
|
ON t.space_id = e.space_id
|
||||||
AND DATE(t.start_time) = e.missing_date
|
AND DATE(t.start_time) = e.missing_date
|
||||||
GROUP BY t.space_id, DATE(t.start_time)
|
GROUP BY 1,2
|
||||||
order by 1,2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SELECT * FROM daily_total_occupancy;
|
SELECT * FROM daily_total_occupancy;
|
Reference in New Issue
Block a user