0
假设我有此表寻找失踪号码顺序int类型的SQL Server
BookNo. | page
001 | 01
001 | 02
001 | 04
002 | 01
002 | 03
003 | 01
003 | 02
是否有可能得到与缺页号码booknos?
假设我有此表寻找失踪号码顺序int类型的SQL Server
BookNo. | page
001 | 01
001 | 02
001 | 04
002 | 01
002 | 03
003 | 01
003 | 02
是否有可能得到与缺页号码booknos?
这将工作,除了第1页缺失(写在甲骨文这样的语法可能会稍有不同):
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 |
我们可以假设的最高页码一本书总是在数据集中,页码始终以1开头? –
'GROUP BY [BookNo。] HAVING COUNT(*)<> MAX(page)'。 –
是的,但每本书都有不同数量的页 –