我正在尝试获取只有零的所有Id
行。
Id | valid
---| ------
1 | 0
1 | 1
2 | 0
2 | 0
3 | 1
4 | 0
4 | 1
5 | 0
6 | 1
答案 0 :(得分:1)
SELECT ID
FROM myTable
WHERE Valid = 0
AND ID NOT IN (
SELECT ID
FROM myTable
WHERE Valid != 0
)
答案 1 :(得分:0)
如果您的Id只有零,那么有效总和将为零。以下是代码示例。
drop table if exists dbo.Table03;
create table dbo.Table03 (
Id int
, valid int
);
insert into dbo.Table03 (Id, valid)
values (1, 0), (1, 1), (2, 0), (2, 0), (3, 1), (4, 0), (4, 1), (5, 0), (6, 1);
select
t.Id
from dbo.Table03 t
group by t.Id
having SUM(t.valid) = 0
答案 2 :(得分:0)
您可以LEFT JOIN
使用NULL
检查来达到预期效果。
使用给定数据执行示例:
DECLARE @TestTable TABLE (Id int, valid int);
INSERT INTO @TestTable (Id, valid)
VALUES (1, 0), (1, 1), (2, 0), (2, 0), (3, 1), (4, 0), (4, 1), (5, 0), (6, 1);
SELECT T1.*
FROM @TestTable T1
LEFT JOIN @TestTable T2 ON T2.Id = T1.Id AND T2.valid <> 0
WHERE T2.Id IS NULL
结果:
Id valid
----------
2 0
2 0
5 0
如果您只想Id
,请使用SELECT DISTINCT T1.Id
。
答案 3 :(得分:0)
又一个选择
Select A.*
From YourTable A
Join (
Select ID
From YourTable
Group By ID
Having min(valid)=0 and max(valid)=0
) B
on A.ID=B.ID
返回
Id valid
2 0
2 0
5 0