私はこのようなデータベース構造を持っています
rowid deltaValue Applicable
1 r n/d
1 w n/d
1 m n/d
2 r n/d
2 w n/d
2 m n/d
3 r n/d
3 w n/r
3 m n/d
基本的に、「rowid」の最後のグループ、つまりrowid=3
. これは、以下の組み合わせを持つ唯一のグループだからです。n/d, n/r
組み合わせを調べてグループ (つまり、ROWID) を取得するだけの tsql クエリはありますか。これが私がこれまでに持っているものです:
select *
from table
where 1=1
and deltaValue in ('r','w','m')
and (( 1=(case when [DeltaValue] = 'r' and [Applicable]='n/r' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'w' and [Applicable]='n/d' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'm' and [Applicable]='n/d' then 1 else 0 end)
) OR
( 1=(case when [DeltaValue] = 'r' and [Applicable]='n/d' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'w' and [Applicable]='n/r' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'm' and [Applicable]='n/d' then 1 else 0 end)
) OR
( 1=(case when [DeltaValue] = 'r' and [Applicable]='n/d' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'w' and [Applicable]='n/d' then 1 else 0 end)
and 1=(case when [DeltaValue] = 'm' and [Applicable]='n/r' then 1 else 0 end)
)
)
出力:
3 r n/d
3 w n/r
3 m n/d