我正在尝试编写一个查询,它将从Bill表中撤消最近的两行,其中Estimated标志为true.问题在于这些需要是连续的账单.
简而言之,如果估算了最后两个账单周期的账单,我需要在另一个表格中输入一行.
如果可能的话,我想在没有光标的情况下这样做,因为我正在使用大量的数据,这必须经常运行.
编辑
表格上有一个AUTOINCREMENT(1,1)列.在没有放弃太多表结构的情况下,表基本上是结构:
CREATE TABLE Bills ( BillId INT AUTOINCREMENT(1,1,) PRIMARY KEY, Estimated BIT NOT NULL, InvoiceDate DATETIME NOT NULL )
所以你可能有一组结果,如:
BillId AccountId Estimated InvoiceDate -------------------- -------------------- --------- ----------------------- 1111196 1234567 1 2008-09-03 00:00:00.000 1111195 1234567 0 2008-08-06 00:00:00.000 1111194 1234567 0 2008-07-03 00:00:00.000 1111193 1234567 0 2008-06-04 00:00:00.000 1111192 1234567 1 2008-05-05 00:00:00.000 1111191 1234567 0 2008-04-04 00:00:00.000 1111190 1234567 1 2008-03-05 00:00:00.000 1111189 1234567 0 2008-02-05 00:00:00.000 1111188 1234567 1 2008-01-07 00:00:00.000 1111187 1234567 1 2007-12-04 00:00:00.000 1111186 1234567 0 2007-11-01 00:00:00.000 1111185 1234567 0 2007-10-01 00:00:00.000 1111184 1234567 1 2007-08-30 00:00:00.000 1111183 1234567 0 2007-08-01 00:00:00.000 1111182 1234567 1 2007-07-02 00:00:00.000 1111181 1234567 0 2007-06-01 00:00:00.000 1111180 1234567 1 2007-05-02 00:00:00.000 1111179 1234567 0 2007-03-30 00:00:00.000 1111178 1234567 1 2007-03-02 00:00:00.000 1111177 1234567 0 2007-02-01 00:00:00.000 1111176 1234567 1 2007-01-03 00:00:00.000 1111175 1234567 0 2006-11-29 00:00:00.000
在这种情况下,只有记录1111188和1111187是连续的.
假设行具有顺序ID,那么您可能正在寻找以下内容:
select top 1 * from Bills b1 inner join Bills b2 on b1.id = b2.id - 1 where b1.IsEstimate = 1 and b2.IsEstimate = 1 order by b1.BillDate desc