数一个岛及其起点和终点

Count an island and its start and end

我有一个 table 看起来像这样:

我想要的是根据 Day_Ranking 列计算数据孤岛,因此输出看起来类似于:

Person           Start Date           End Date           Count
 45286            2021-08-26           2021-09-07         8
 13914            2021-09-22           2021-10-01         7
 32914            2021-01-10           2021-01-10         1
 32914            2021-01-12           2021-01-13         2

有什么关于如何做到这一点的建议吗? 我正在使用 Oracle SQL

从 Oracle 12 开始,您可以使用 MATCH_RECOGNIZE 执行 row-by-row 比较:

SELECT *
FROM   table_name
MATCH_RECOGNIZE(
  PARTITION BY person
  ORDER BY day_ranking
  MEASURES
    FIRST(date_value) AS start_date,
    LAST(date_value) AS end_date,
    COUNT(*) AS count
  ONE ROW PER MATCH
  PATTERN (successive_ranks* last_rank)
  DEFINE successive_ranks AS day_ranking + 1 = NEXT(day_ranking)
)

其中,对于示例数据:

CREATE TABLE table_name (person, date_value, day_ranking) AS
SELECT 1, DATE '2022-01-01',  6 FROM DUAL UNION ALL
SELECT 1, DATE '2022-01-02',  7 FROM DUAL UNION ALL
SELECT 1, DATE '2022-01-04',  8 FROM DUAL UNION ALL
SELECT 1, DATE '2022-01-05',  9 FROM DUAL UNION ALL
SELECT 1, DATE '2022-01-07', 10 FROM DUAL UNION ALL
SELECT 1, DATE '2022-01-09', 11 FROM DUAL UNION ALL
SELECT 2, DATE '2022-01-01', 15 FROM DUAL UNION ALL
SELECT 2, DATE '2022-01-02', 16 FROM DUAL UNION ALL
SELECT 2, DATE '2022-01-03', 17 FROM DUAL UNION ALL
SELECT 2, DATE '2022-01-04', 18 FROM DUAL UNION ALL
SELECT 2, DATE '2022-01-05', 19 FROM DUAL UNION ALL
SELECT 3, DATE '2022-01-01', 25 FROM DUAL UNION ALL
SELECT 3, DATE '2022-01-03', 27 FROM DUAL;

输出:

PERSON START_DATE END_DATE COUNT
1 01-JAN-22 09-JAN-22 6
2 01-JAN-22 05-JAN-22 5
3 01-JAN-22 01-JAN-22 1
3 03-JAN-22 03-JAN-22 1

db<>fiddle here