SQL-日期范围中缺少时间

时间:2019-06-10 05:16:43

标签: sql postgresql date-range gaps-and-islands

我在一个表中有显示点火truefalse

的数据
|--------------------------------------|
|             date          | ignition |
|---------------------------|----------|
|2019-06-03 10:15:00.000000 | false    |
|---------------------------|----------|
|2019-06-03 10:30:00.000000 | false    |
|---------------------------|----------|
|2019-06-03 11:30:00.000000 | true     |
|---------------------------|----------|
|2019-06-03 11:45:00.000000 | false    |
|---------------------------|----------|
|2019-06-03 11:55:00.000000 | false    |
|---------------------------|----------|

我编写了SQL查询来显示日期期间的点火状态

SELECT min(date) as date_from, max(date) as date_to, ignition
FROM (SELECT date,
             ignition,
             row_number() over (order by date) as seqnum,
             row_number() over (partition by ignition order by date) as seqnum_s
      FROM table_data
      WHERE date >= '2019-06-01T21:00:00.000Z' AND date <= '2019-06-09T20:59:59.999Z'
    ) as raw_data
GROUP BY (seqnum - seqnum_s), ignition
ORDER BY min(date);

查询结果:

|---------------------------|----------------------------|----------|
|             date_from     | date_to                    | ignition |
|---------------------------|----------------------------|----------|
|2019-06-03 10:15:00.000000 | 2019-06-03 10:30:00.000000 | false    |
|---------------------------|----------------------------|----------|
|2019-06-03 11:30:00.000000 | 2019-06-03 11:30:00.000000 | true     |
|---------------------------|----------------------------|----------|
|2019-06-03 11:45:00.000000 | 2019-06-03 11:55:00.000000 | false    |
|---------------------------|----------------------------|----------|

但是结果中缺少从10:3011:30的日期和从11:3011:45的日期。我应该得到这个:

|---------------------------|----------------------------|----------|
|             date_from     | date_to                    | ignition |
|---------------------------|----------------------------|----------|
|2019-06-03 10:15:00.000000 | 2019-06-03 11:30:00.000000 | false    |
|---------------------------|----------------------------|----------|
|2019-06-03 11:30:00.000000 | 2019-06-03 11:45:00.000000 | true     |
|---------------------------|----------------------------|----------|
|2019-06-03 11:45:00.000000 | 2019-06-03 11:55:00.000000 | false    |
|---------------------------|----------------------------|----------|

问题在于,在聚合为一个块(例如,后面的5行错误行)期间,他仅查看错误的最后一行来确定该块的结束时间,而不是接下来的第一行(第一行为true)

2 个答案:

答案 0 :(得分:1)

好的,只需要使用窗口函数lead来获取下一行的值date_from,然后将此值设置为当前行的date_to,则如下所示的SQL:

select 
    date_from,
    case when lead(date_from,1) over(order by date_from) is null then date_to else lead(date_from,1) over(order by date_from) end as date_to,
    ignition
from (
SELECT min(date) as date_from, max(date) as date_to, ignition
FROM (SELECT date,
             ignition,
             row_number() over (order by date) as seqnum,
             row_number() over (partition by ignition order by date) as seqnum_s
      FROM table_data
      WHERE date >= '2019-06-01T21:00:00.000Z' AND date <= '2019-06-09T20:59:59.999Z'
    ) as raw_data
GROUP BY (seqnum - seqnum_s), ignition
ORDER BY min(date)
) tmp;
      date_from      |       date_to       | ignition 
---------------------+---------------------+----------
 2019-06-03 10:15:00 | 2019-06-03 11:30:00 | f
 2019-06-03 11:30:00 | 2019-06-03 11:45:00 | t
 2019-06-03 11:45:00 | 2019-06-03 11:55:00 | f
(3 rows)

答案 1 :(得分:0)

一种简单的表达方式是:

SELECT min(date) as date_from,
       lead(min(date)) over (order by min(date)) as date_to,
       ignition
FROM (SELECT d.*,
             row_number() over (order by date) as seqnum,
             row_number() over (partition by ignition order by date) as seqnum_s
      FROM table_data d
      WHERE date >= '2019-06-01T21:00:00.000Z' AND date < '2019-06-09T21:00:00Z'
     ) as raw_data
GROUP BY (seqnum - seqnum_s), ignition
ORDER BY min(date)