首页 > 解决方案 > 仅当其中没有特定值时才选择数据

问题描述

我们有一个通过 sql thru ms 访问运行的遗留案例管理。

我们的表格示例如下所示

桌子

有没有办法只在没有特定 SNUM 的情况下拉取或选择显示特定数据。

就像我们有多个 snum 条目的 casenum。但我们只想选择从未有来自某个 snum 条目的 casenum

因此,例如,如果有一个 casenum 的条目仅来自 151 和 152,但没有来自 153 和 154 的条目,那么它将显示。

但如果 casenum 有来自 snum 153 或 154 的条目,则它不会显示它

标签: sqlif-statementselect

解决方案


select casenum 
from your_table
group by casenum 
having sum(case when snum in (153, 154) then 1 else 0 end) = 0
   and sum(case when snum = 151 then 1 else 0 end) >= 1
   and sum(case when snum = 152 then 1 else 0 end) >= 1

推荐阅读