简体   繁体   English

获取序列中所有缺失的数字

[英]Get all missing numbers in the sequence

The numbers are originally alpha numeric so I have a query to parse out the numbers: 数字最初是字母数字,所以我有一个查询来解析数字:

My query here gives me a list of numbers: 我的查询在这里给了我一个数字列表:

select distinct cast(SUBSTRING(docket,7,999) as INT) from [DHI_IL_Stage].[dbo].[Violation] where InsertDataSourceID='40' and ViolationCounty='Carroll' and SUBSTRING(docket,5,2)='TR' and LEFT(docket,4)='2011' order by 1 从[DHI_IL_Stage]。[dbo]。[Violation]中选择不同的演员表(SUBSTRING(docket,7,999)作为INT)。并且LEFT(docket,4)='2011'的顺序是1

Returns the list of numbers parsed out. 返回解析出的数字列表。 For example, the number will be 2012TR557. 例如,该号码将为2012TR557。 After using the query it will be 557. 使用查询后将是​​557。

I need to write a query that will give back the missing numbers in a sequence. 我需要编写一个查询,该查询将按顺序返回丢失的数字。

Here is one approach The following should return one row for each sequence of missing numbers. 这是一种方法。以下应为丢失的数字的每个序列返回一行。 So, if you series is 3, 5, 6, 9, then it should return: 因此,如果您的序列是3、5、6、9,那么它应该返回:

4     4
7     8

The query is: 查询是:

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM