这些数字最初是字母数字,所以我有一个查询来解析数字:
我的查询在这里给出了一个数字列表:
从中选择不同的强制转换(SUBSTRING(docket,7,999)为INT) [DHI_IL_Stage]。[dbo]。[违规]其中InsertDataSourceID ='40'和 ViolationCounty ='Carroll'和SUBSTRING(docket,5,2)='TR'和 LEFT(案卷号,4)='2011'按1的顺序
返回解析出的数字列表。 例如,号码将是2012TR557。使用查询后,它将是557。
我需要编写一个查询来回复序列中缺少的数字。
答案 0 :(得分:0)
这是一种方法 对于每个缺失数字序列,以下内容应返回一行。所以,如果你的系列是3,5,6,9,那么它应该返回:
4 4
7 8
查询是:
with nums as (
select distinct cast(SUBSTRING(docket, 7, 999) as INT) as n,
row_number() over (order by cast(SUBSTRING(docket, 7, 999) as INT)) as seqnum
from [DHI_IL_Stage].[dbo].[Violation]
where InsertDataSourceID = '40' and
ViolationCounty = 'Carroll' and
SUBSTRING(docket,5,2) = 'TR' and
LEFT(docket, 4) = '2011'
)
select (nums_prev.n + 1) as first_missing, nums.n - 1 as last_missing
from nums left outer join
nums nums_prev
on nums.seqnum = nums_prev.seqnum + 1
where nums.n <> nums_prev.n + 1 ;