2

鉴于以下设置:

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)

4

2 回答 2

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尽管这仅在for each只能有一个组合时才有效name

于 2012-10-17T21:33:09.907 回答
1

你甚至可以使用相交

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
于 2012-10-18T04:44:37.617 回答