diff --git a/timeBasedReport.sql b/timeBasedReport.sql index 9356322..b624fe4 100644 --- a/timeBasedReport.sql +++ b/timeBasedReport.sql @@ -1,6 +1,5 @@ -- ============================================================================= -- Отчет повременного учета работоспособности периферийного оборудования --- Период: май 2026 -- Исключены устройства, начинающиеся на IRZ -- Тип оборудования берется из инвентаризации Zabbix (поле type) -- Координаты: широта + долгота из полей location_lat и location_lon @@ -64,10 +63,10 @@ checks_with_prev AS ( SELECT *, LAG(status) OVER (PARTITION BY host_name ORDER BY ts) AS prev_status, - coalesce(lead(ts) OVER (PARTITION BY host_name ORDER BY ts), date_trunc('day', ts) + interval '1 day - 1 sec') AS next_ts + coalesce(lead(ts) OVER (PARTITION BY host_name ORDER BY ts), date_trunc('day', ts) + interval '1 day') AS next_ts FROM checks ), ---select * from ( + -- 3. Определяем моменты смены статуса (начало нового интервала) status_changes AS ( SELECT @@ -80,16 +79,12 @@ status_changes AS ( status, prev_status, case - when DATE(next_ts) > DATE(ts) then date_trunc('day', ts) + interval '23:59:59' + when DATE(next_ts) > DATE(ts) then date_trunc('day', next_ts) + interval '00:00:00' else next_ts - interval '1 sec' end as next_ts, CASE WHEN status != prev_status THEN 1 ELSE 0 - END AS is_start, - case - when DATE(next_ts) > DATE(ts) then 1 - else 0 - end as is_24hh + END AS is_start FROM checks_with_prev union all SELECT @@ -101,11 +96,10 @@ status_changes AS ( date_trunc('day', next_ts) + interval '00:00:00' as ts, status, prev_status, - next_ts - interval '1 sec', - 1 as is_start, - 0 as is_24hh + next_ts - interval '1 sec' as next_ts, + 1 as is_start FROM checks_with_prev - where DATE(next_ts) > DATE(ts) --)f where status != prev_status and date_trunc('sec', ts) = date_trunc('sec', next_ts) + where DATE(next_ts) > DATE(ts) ), -- 4. Нумеруем интервалы для каждого устройства @@ -129,8 +123,7 @@ grouped_intervals AS ( max(case when object_number similar to '%№%' then regexp_replace(object_number, 'ОБЪЕКТ №\s*', '', 'g')::int else null end) as object_num, MIN(ts) AS time_start, MAX(next_ts) AS time_end, - MAX(status) AS status, - MAX(is_24hh) AS is_24hh + MAX(status) AS status FROM intervals GROUP BY host_name, @@ -148,9 +141,8 @@ SELECT equipment_type as "Тип оборудования", case when object_num is null then 'на инвентаризации' else object_number end as "Номер_объекта_ИТС_РО", coordinates as "Координаты_расположения", - TO_CHAR(time_start, 'yyyy-mm-dd HH24:MI:SS')::varchar AS "Время начала статуса", - case when is_24hh = 1 then TO_CHAR(DATE(time_end), 'yyyy-mm-dd')::varchar || ' 24:00:00' - else time_end::varchar end as "Время окончания статуса", + TO_CHAR(time_start, 'HH24:MI:SS') AS "Время начала статуса", + TO_CHAR(time_end, 'HH24:MI:SS') as "Время окончания статуса", CASE WHEN status = 1 THEN 'доступно' ELSE 'недоступно'