我们可以通过几个分析来做到这一点:
SQL> alter session set nls_date_format='yy/mm/dd';
Session altered.
SQL> select id, min(val) date_from, max(val) date_till
2 from (select id, val, max(grp) over(partition by id order by val) grp
3 from (select id, val, lag(val, 1) over(partition by id order by val),
4 case
5 when lag(val, 1) over(partition by id order by val) < val - 1 then
6 row_number() over(partition by id order by val)
7 when row_number() over(partition by id order by val) = 1 then
8 1
9 end grp
10 from mytab))
11 group by id, grp
12 order by id, date_from
13 /
ID DATE_FRO DATE_TIL
---------- -------- --------
9465 12/12/20 12/12/22
9465 12/12/25 12/12/26
9466 12/12/21 12/12/25
9466 12/12/27 12/12/27
即首先我们将结果集分成组,其中组被定义为给定ID 的连续日期。我们通过检查之前的日期并查看它是否 < 当前行 date - 1 来做到这一点lag(val, 1) over(partition by id order by val)
SQL> select id, val, lag(val, 1) over(partition by id order by val),
2 case
3 when lag(val, 1) over(partition by id order by val) < val - 1 then
4 row_number() over(partition by id order by val)
5 when row_number() over(partition by id order by val) = 1 then
6 1
7 end grp
8 from mytab
9 /
ID VAL LAG(VAL, GRP
---------- -------- -------- ----------
9465 12/12/20 1
9465 12/12/21 12/12/20
9465 12/12/22 12/12/21
9465 12/12/25 12/12/22 4
9465 12/12/26 12/12/25
9466 12/12/21 1
9466 12/12/22 12/12/21
9466 12/12/23 12/12/22
9466 12/12/24 12/12/23
9466 12/12/25 12/12/24
9466 12/12/27 12/12/25 6
11 rows selected.
我们接下来需要填写空白,以便空白与之前的非空组相关联。即,我们将 max() 应用到这里max(grp) over(partition by id order by val)
的顺序,这意味着我们只取到该点为止看到的最大行,而不是整个集合中的最大值。
SQL> select id, val, max(grp) over(partition by id order by val) grp
2 from (select id, val, lag(val, 1) over(partition by id order by val),
3 case
4 when lag(val, 1) over(partition by id order by val) < val - 1 then
5 row_number() over(partition by id order by val)
6 when row_number() over(partition by id order by val) = 1 then
7 1
8 end grp
9 from mytab)
10 /
ID VAL GRP
---------- -------- ----------
9465 12/12/20 1
9465 12/12/21 1
9465 12/12/22 1
9465 12/12/25 4
9465 12/12/26 4
9466 12/12/21 1
9466 12/12/22 1
9466 12/12/23 1
9466 12/12/24 1
9466 12/12/25 1
9466 12/12/27 6
11 rows selected.
现在它是一个简单的 group by (id, grp) 被应用,为每个 id+group 取 min() 和 max()。