如何使用想要的跳过检查缺少的数字序列
Posted
技术标签:
【中文标题】如何使用想要的跳过检查缺少的数字序列【英文标题】:How to check missing number sequence with wanted skips 【发布时间】:2017-07-03 09:32:39 【问题描述】:基于:How to check any missing number from a series of numbers?
我有一个类似的问题。我的源表有一个从 1 到 1000 的序列。 但只有当差距大于 1 且小于 20 时,它才是坏的。我无法让 CONNECT BY 工作。
请帮帮我。
SELECT
'XX' AS NETWORK
,'YY' AS TYPE
,min_seq - 1 + level AS MISSING
FROM (
select
min(s.SEQUENCE_NUMBER) min_seq
, max(s.SEQUENCE_NUMBER) max_seq
FROM source s
)
CONNECT BY level <= max_seq - min_seq +20 AND level >= max_seq - min_seq +1
MINUS
SELECT
'XX' AS NETWORK
,'YY' AS TYPE
,s.SEQUENCE_NUMBER AS EXISTING
FROM source s
【问题讨论】:
【参考方案1】:老派connect by
版
with tn as(
-- sample data
Select 1 n from dual
union all
Select 4 from dual
union all
Select 26 from dual
union all
Select 30 from dual
union all
Select 52 from dual
)
select distinct n, delta, n+level nn
from (
select n, delta
from (
select n, lead(n) Over(order by n) - n delta
from tn) t
where delta between 2 and 20
) t2
connect by level < delta
order by n
【讨论】:
嗨,Serg,尝试,修改,成功!非常感谢!【参考方案2】:使用 CTE(with 语句):
with CTE as
(
select level as NN
from dual
connect by level <= 20
)
select CTE.NN
from CTE
left join source s
on CTE.NN = s.SEQUENCE_NUMBER
where s.SEQUENCE_NUMBER is null
【讨论】:
以上是关于如何使用想要的跳过检查缺少的数字序列的主要内容,如果未能解决你的问题,请参考以下文章