如何检查SEQ没有' 0'记录

时间:2015-06-24 15:01:49

标签: sql sql-server

表A

ID      SEQ 
123      0
123      1
123      2
123      3
234      2
234      4
234      5
345      2
345      3
345      4

现在我需要检查不具有SEQ = 0的ID。在上面的示例中,234345没有0

3 个答案:

答案 0 :(得分:3)

<span class="c1" > text1 text2 text3 </span> text4 text5 text6... <span class="c2" > text8 text9 text10 </span> text14 text15 text16... <span class="c1" > text18 text19 text20 </span> text21

答案 1 :(得分:3)

您可以使用NOT EXISTS子句过滤ID:

SELECT DISTINCT ID
FROM TableA a
WHERE NOT EXISTS (SELECT ID FROM TableA b WHERE SEQ = 0 AND a.ID = b.ID)

Runnable Sample:

CREATE TABLE #TableA ( ID INT, SEQ INT )

INSERT  INTO #TableA
        ( ID, SEQ )
VALUES  ( 123, 0 ),
        ( 123, 1 ),
        ( 123, 2 ),
        ( 123, 3 ),
        ( 234, 2 ),
        ( 234, 4 ),
        ( 234, 5 ),
        ( 345, 2 ),
        ( 345, 3 ),
        ( 345, 4 )

SELECT DISTINCT
        ID
FROM    #TableA a
WHERE   NOT EXISTS ( SELECT ID
                     FROM   #TableA b
                     WHERE  SEQ = 0
                            AND a.ID = b.ID )

DROP TABLE #TableA

产地:

ID
===
234
345

答案 2 :(得分:0)

您可以使用GROUP BY

SELECT ID
FROM mytable
GROUP BY ID
HAVING COUNT(CASE WHEN SEQ = 0 THEN 1 END) = 0