如果主条目不存在,如何在表中跳过几个条目?

时间:2016-11-23 12:33:52

标签: sql-server sql-server-2012

我有下表 输入 在这里,我试图跳过几个基于主条目的条目,请在附图中找到更多细节。

Image1

Image2

主表是从许多表生成的。

很抱歉没有提供查询和表格的正确快照,因为我不应该在线分享它们。

由于

1 个答案:

答案 0 :(得分:1)

DECLARE @T TABLE 
                (CLIENTID INT,
                STATUSID NVARCHAR(200),
                STATUS NVARCHAR(200),
                VALUE NVARCHAR(200),
                CREATEDDATE DATETIME)

INSERT INTO @T 
VALUES  (25,100857,'SCHEME STATUS','CLIENT FOUND ELIGIBLE','2016-11-23 15:18:38:000'),
        (25,100858,'REASON FOR DECLINING TO APPLY','TOO UNWELL','2016-11-23 15:18:38:000'),
        (25,100858,'REASON FOR DECLINING TO APPLY','NOT INTRESTED','2016-11-23 15:18:38:000'),
        (25,100858,'REASON FOR DECLINING TO APPLY','UNDECIDED','2016-11-23 15:18:38:000'),
        (25,100859,'ELIGIBLE TO APPLY','YES','2016-11-23 15:18:38:000'),
        (26,100857,'SCHEME STATUS','CLIENT NOT FOUND ELIGIBLE','2016-11-23 15:18:38:000'),
        (26,100858,'REASON FOR DECLINING TO APPLY','TOO UNWELL','2016-11-23 15:18:38:000'),
        (26,100858,'REASON FOR DECLINING TO APPLY','NOT INTRESTED','2016-11-23 15:18:38:000'),
        (26,100858,'REASON FOR DECLINING TO APPLY','UNDECIDED','2016-11-23 15:18:38:000'),
        (26,100859,'ELIGIBLE TO APPLY','YES','2016-11-23 15:18:38:000')

SELECT * FROM @T 
WHERE CLIENTID IN (SELECT CLIENTID FROM @T WHERE STATUS = 'SCHEME STATUS' AND  VALUE = 'CLIENT FOUND ELIGIBLE') OR 
      STATUSID NOT IN (SELECT STATUSID+1 FROM @T WHERE STATUS = 'SCHEME STATUS' AND  VALUE = 'CLIENT NOT FOUND ELIGIBLE')