假设我有这张表
BookNo. | page
001 | 01
001 | 02
001 | 04
002 | 01
002 | 03
003 | 01
003 | 02
是否可以获取缺少页码的书籍?
答案 0 :(得分:0)
除非缺少第1页(在oracle上编写,因此语法可能略有不同),否则这将有效:
with cte1 as (
select distinct
BookNo
,case when a.page + 1 != lead(page) over (partition by bookno order by page) then page + 1 end as missing_start
,case when a.page + 1 != lead(page) over (partition by bookno order by page) then lead(page) over (partition by bookno order by page) - 1 end as missing_end
from test a
where 1=1
)
select *
from cte1
where missing_start is not null
order by 1
;
结果:
| BOOKNO | MISSING_START | MISSING_END |
|--------|---------------|-------------|
| 001 | 3 | 3 |
| 002 | 2 | 2 |