我有一个这样的数据库结构
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
是否有一个 tsql 查询将只查看组合并拉取组(即 rowid)。这是我到目前为止所拥有的:
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