mirror of
https://github.com/SyncrowIOT/data.git
synced 2025-07-16 01:56:20 +00:00
fixed duration calculation
This commit is contained in:
@ -35,9 +35,10 @@ time_differences AS (
|
|||||||
event_time::date AS event_date,
|
event_time::date AS event_date,
|
||||||
EXTRACT(EPOCH FROM (event_time - COALESCE(prev_timestamp, event_time))) AS time_diff_in_seconds
|
EXTRACT(EPOCH FROM (event_time - COALESCE(prev_timestamp, event_time))) AS time_diff_in_seconds
|
||||||
FROM start_date
|
FROM start_date
|
||||||
)
|
),
|
||||||
|
|
||||||
|
|
||||||
|
duration as (
|
||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
event_date,
|
event_date,
|
||||||
@ -45,5 +46,16 @@ SELECT
|
|||||||
SUM(CASE WHEN prev_value = 'presence' THEN time_diff_in_seconds ELSE 0 END) AS presence_seconds,
|
SUM(CASE WHEN prev_value = 'presence' THEN time_diff_in_seconds ELSE 0 END) AS presence_seconds,
|
||||||
SUM(CASE WHEN prev_value = 'none' THEN time_diff_in_seconds ELSE 0 END) AS none_seconds
|
SUM(CASE WHEN prev_value = 'none' THEN time_diff_in_seconds ELSE 0 END) AS none_seconds
|
||||||
FROM time_differences
|
FROM time_differences
|
||||||
|
WHERE prev_timestamp::date=event_date
|
||||||
GROUP BY device_id, event_date
|
GROUP BY device_id, event_date
|
||||||
ORDER BY device_id, event_date
|
ORDER BY device_id, event_date)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
select device_id,
|
||||||
|
event_date,
|
||||||
|
CONCAT(FLOOR(motion_seconds / 3600), ':', LPAD(FLOOR((motion_seconds % 3600) / 60)::TEXT, 2, '0')) AS motion_formated_duration,
|
||||||
|
CONCAT(FLOOR(presence_seconds / 3600), ':', LPAD(FLOOR((presence_seconds % 3600) / 60)::TEXT, 2, '0')) AS motion_formated_duration,
|
||||||
|
CONCAT(FLOOR(none_seconds / 3600), ':', LPAD(FLOOR((none_seconds % 3600) / 60)::TEXT, 2, '0')) AS motion_formated_duration
|
||||||
|
from duration
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user