根据一列将连续的行分组

时间:2019-04-12 14:42:21

标签: sql postgresql gaps-and-islands

假设我从select * from journeys的结果获得了这张桌子:

timestamp     | inJourney (1 = true and 0 = false)
--------------------------------------------------
time1         | 1
time2         | 1
time3         | 1
time4         | 0
time5         | 0
time6         | 1
time7         | 1
time8         | 1

预期:

timestamp     | inJourney (1 = true and 0 = false)
--------------------------------------------------
time1         | 1
time4         | 0
time8         | 1

注意:时间戳并不重要,因为我只想计算出旅行的次数。

知道要做什么吗?

2 个答案:

答案 0 :(得分:2)

这是一个孤岛问题。使用row_number()的区别:

select injourney, min(timestamp), max(timestamp)
from (select t.*,
             row_number() over (order by timestamp) as seqnum,
             row_number() over (partition by injourney, order by timestamp) as seqnum_i
      from t
     ) t
group by injourney, (seqnum - seqnum_i)
order by min(timestamp);

答案 1 :(得分:2)

这是一个空白问题,您可以尝试使用ROW_NUMBER窗口函数从结果集中获取空白,然后使用MIN

您可以尝试一下。

查询#1

SELECT MIN(timestamp),inJourney 
FROM (
SELECT *,
    ROW_NUMBER() OVER(ORDER BY timestamp)  - ROW_NUMBER() OVER(PARTITION BY inJourney ORDER BY timestamp) grp
  FROM journeys
) t1
GROUP BY grp,inJourney 
ORDER BY MIN(timestamp);

| min   | injourney |
| ----- | --------- |
| time1 | 1         |
| time4 | 0         |
| time6 | 1         |

View on DB Fiddle