给出以下设置:
DECLARE @TemplateInfo table (Name varchar(10), aRow int, aCol int, Value int)
insert INTO @TemplateInfo (Name, aRow, aCol, Value)
VALUES ('A', 0,0,1),('B', 0,0,2),('A',1,0,2),('B',1,0,1),('C',0,0,1),('C',1,0,1)
如何选择具有(aRow = 0,aCol = 0,Value = 1)AND(aRow = 1,aCol = 0,Value = 1)的唯一名称名称值。
我尝试了这样的查询:
select Name
from @TemplateInfo info
where (info.aRow = 0 and info.aCol = 0 AND Value = 1)
or (info.aRow = 1 and info.aCol = 0 AND Value = 1)
GROUP BY Name
但是返回A,B和C.我如何检查以确保跨行匹配(仅返回C)
答案 0 :(得分:2)
你真的很亲密。由于您希望评估两个条件,因此可以使用HAVING
:
select Name
from @TemplateInfo info
where (info.aRow = 0 and info.aCol = 0 AND Value = 1)
or (info.aRow = 1 and info.aCol = 0 AND Value = 1)
group by Name
HAVING COUNT(*) = 2
虽然只有每aRow, aCol, Value
只有一个name
组合才有效。
答案 1 :(得分:1)
您甚至可以使用Intersect
Select name From @TemplateInfo where aRow=0 and aCol=0 and Value=1
intersect
Select name From @TemplateInfo where aRow=1 and aCol=0 and Value=1
<强>结果强>
name
C