如何在SQL中检查上一个和下一个记录是否是连续的

时间:2015-04-16 09:04:00

标签: sql sql-server cursor identity

我有一个包含列的表(ID int,RECEIPT_BARCODE nvarchar(200),FK_CLOSURE bigint)

ID          RECEIPT_BARCODE          FK_CLOSURE  

1           01020011304500190001    58100000010019
2           01020011304500190002    58100000010019
1           01020011404500200001    58100000010020
2           01020011404500200002    58100000010020
3           01020011404500200003    58100000010020
1           01020011504500210001    58100000010021
1           01020011604500220001    58100000010022
1           01020011604500230001    58100000010023
2           01020011604500230002    58100000010023
1           01020011604500250001    58100000010025
3           01020011604500250003    58100000010025

我们的程序会自动将销售插入表中。正在插入此表; 当FK_CLOSURE上升时,ID再次被设置为' 1' (正如你在上面看到的那样)只要FK_CLOSURE没有改变就会顺序进行。当FK_CLOSURE再次更改时,ID设置为' 1'。

所以问题是;有时我们的销售计划工作错误,并且doest插入销售但是增加ID值,如您在最后一行中所见。在最后一行,它将ID从1增加到3,没有2 ...

我想报告那种失败。我尝试使用CURSOR但无法管理。 有什么建议 ?

2 个答案:

答案 0 :(得分:2)

您可以使用逻辑来比较行号和键之间的差异,如果差异发生变化,则ID号中会有一个漏洞。在你的情况下,差异应该总是为零。所以像这样:

create table #test (ID int, RECEIPT_BARCODE nvarchar(200),FK_CLOSURE bigint) 

insert into #test values 
(1, '01020011304500190001',58100000010019),
(2, '01020011304500190002',58100000010019),
(1, '01020011404500200001',58100000010020),
(2, '01020011404500200002',58100000010020),
(3, '01020011404500200003',58100000010020),
(1, '01020011504500210001',58100000010021),
(1, '01020011604500220001',58100000010022),
(1, '01020011604500230001',58100000010023),
(2, '01020011604500230002',58100000010023),
(1, '01020011604500250001',58100000010025),
(3, '01020011604500250003',58100000010025)


select 
  ID, 
  RECEIPT_BARCODE, 
  FK_CLOSURE, 
  ID - row_number() over(partition by FK_CLOSURE order by ID asc)
from #test

结果将是:

1   01020011304500190001    58100000010019  0
2   01020011304500190002    58100000010019  0
1   01020011404500200001    58100000010020  0
2   01020011404500200002    58100000010020  0
3   01020011404500200003    58100000010020  0
1   01020011504500210001    58100000010021  0
1   01020011604500220001    58100000010022  0
1   01020011604500230001    58100000010023  0
2   01020011604500230002    58100000010023  0
1   01020011604500250001    58100000010025  0
3   01020011604500250003    58100000010025  1

答案 1 :(得分:1)

尝试此查询

declare  @SalesTable table (ID int, RECEIPT_BARCODE nvarchar(200),FK_CLOSURE bigint)

insert into @SalesTable values 
(1, '01020011304500190001',58100000010019),
(2, '01020011304500190002',58100000010019),
(1, '01020011404500200001',58100000010020),
(2, '01020011404500200002',58100000010020),
(3, '01020011404500200003',58100000010020),
(1, '01020011504500210001',58100000010021),
(1, '01020011604500220001',58100000010022),
(1, '01020011604500230001',58100000010023),
(2, '01020011604500230002',58100000010023),
(1, '01020011604500250001',58100000010025),
(3, '01020011604500250003',58100000010025)

select 
  FK_CLOSURE
from @SalesTable
group by FK_CLOSURE
having count(distinct ID) != MAX(ID)

如果任何FK_CLOSURE缺少某些ID,则{FK_CLOSURE

<{1}}将小于count(distinct ID)

输出

MAX(ID)