仅选择条件的记录组

时间:2017-12-27 20:33:20

标签: tsql

我有一张类似于此的表

   THid Sid TID Sealantid
    1   1   1   1
    2   1   2   1
    3   1   3   4
    4   1   4   1
    5   1   5   1
    6   1   6   1
    33  2   1   1
    34  2   2   1
    35  2   3   1
    36  2   4   1
    37  2   5   1
    38  2   6   1
    65  3   1   1
    66  3   2   1
    67  3   3   4
    68  3   4   1
    69  3   5   1
    70  3   6   1
    97  4   1   1
    98  4   2   1
    99  4   3   8
    100 4   4   1
    101 4   5   1
    102 4   6   1
    129 5   1   1
    130 5   2   1
    131 5   3   8
    132 5   4   1
    133 5   5   1
    134 5   6   1
    161 6   1   1
    162 6   2   1
    163 6   3   4
    164 6   4   1
    165 6   5   1
    166 6   6   1
    193 7   1   1
    194 7   2   1
    195 7   3   4
    196 7   4   1
    197 7   5   1
    198 7   6   1
    225 8   1   1
    226 8   2   1
    227 8   3   4
    228 8   4   1
    229 8   5   1
    230 8   6   1
    257 9   1   1
    258 9   2   1
    259 9   3   1
    260 9   4   1
    261 9   5   1
    262 9   6   1
    289 10  1   1
    290 10  2   1
    291 10  3   4
    292 10  4   1
    293 10  5   1
    294 10  6   1

在这里,我想找到只有Sid的“所有sealantid = 1”的记录

简单查询我试过这个

select * from table where sealantid=1

但是这给了我所有的sid,但我只想要SID,Tid(1到6)所有的sealantid = 1

在此表2,9中

2 个答案:

答案 0 :(得分:1)

您可以将NOT EXISTS与子查询一起使用

试试这个:

WITH SampleData AS (
     SELECT V.*
     FROM (VALUES 
     (1,   1,   1,   1)
    ,(2,   1,   2,   1)
    ,(3,   1,   3,   4)
    ,(4,   1,   4,   1)
    ,(5,   1,   5,   1)
    ,(6,   1,   6,   1)
    ,(33,  2,   1,   1)
    ,(34,  2,   2,   1)
    ,(35,  2,   3,   1)
    ,(36,  2,   4,   1)
    ,(37,  2,   5,   1)
    ,(38,  2,   6,   1)
    ,(65,  3,   1,   1)
    ,(66,  3,   2,   1)
    ,(67,  3,   3,   4)
    ,(68,  3,   4,   1)
    ,(69,  3,   5,   1)
    ,(70,  3,   6,   1)
    ,(97,  4,   1,   1)
    ,(98,  4,   2,   1)
    ,(99,  4,   3,   8)
    ,(100, 4,   4,   1)
    ,(101, 4,   5,   1)
    ,(102, 4,   6,   1)
    ,(129, 5,   1,   1)
    ,(130, 5,   2,   1)
    ,(131, 5,   3,   8)
    ,(132, 5,   4,   1)
    ,(133, 5,   5,   1)
    ,(134, 5,   6,   1)
    ,(161, 6,   1,   1)
    ,(162, 6,   2,   1)
    ,(163, 6,   3,   4)
    ,(164, 6,   4,   1)
    ,(165, 6,   5,   1)
    ,(166, 6,   6,   1)
    ,(193, 7,   1,   1)
    ,(194, 7,   2,   1)
    ,(195, 7,   3,   4)
    ,(196, 7,   4,   1)
    ,(197, 7,   5,   1)
    ,(198, 7,   6,   1)
    ,(225, 8,   1,   1)
    ,(226, 8,   2,   1)
    ,(227, 8,   3,   4)
    ,(228, 8,   4,   1)
    ,(229, 8,   5,   1)
    ,(230, 8,   6,   1)
    ,(257, 9,   1,   1)
    ,(258, 9,   2,   1)
    ,(259, 9,   3,   1)
    ,(260, 9,   4,   1)
    ,(261, 9,   5,   1)
    ,(262, 9,   6,   1)
    ,(289, 10,  1,   1)
    ,(290, 10,  2,   1)
    ,(291, 10,  3,   4)
    ,(292, 10,  4,   1)
    ,(293, 10,  5,   1)
    ,(294, 10,  6,   1)
    ) AS V (THid, Sid, TID, Sealantid)
)
SELECT DISTINCT SD.Sid
FROM SampleData AS SD
WHERE NOT EXISTS (
  SELECT 1 FROM SampleData AS C
  WHERE SD.Sid = C.Sid AND C.Sealantid <> 1
)

You can try it on fiddle

替代方案可以是LEFT JOIN或NOT IN

SELECT DISTINCT SD.Sid
FROM SampleData AS SD 
LEFT JOIN (
SELECT DISTINCT Sid FROM SampleData WHERE Sealantid <> 1
) AS C
ON SD.Sid = C.Sid
WHERE c.Sid IS NULL

SELECT DISTINCT SD.Sid
FROM SampleData AS SD 
WHERE SD.Sid NOT IN ( 
SELECT DISTINCT Sid FROM SampleData WHERE Sealantid <> 1
) 

答案 1 :(得分:1)

你可以使用gruop:

 Select sid
 From table
 Group by sid
 Having min(sealantId) = 1
 And max(sealantId) = 1