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) を持つ一意の Name 値を選択するにはどうすればよいですか。

次のようなクエリを試しました。

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の組み合わせが 1 つしかない場合にのみ有効です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 に答える