sql - 仅当其中没有特定值时才选择数据
问题描述
我们有一个通过 sql thru ms 访问运行的遗留案例管理。
我们的表格示例如下所示
有没有办法只在没有特定 SNUM 的情况下拉取或选择显示特定数据。
就像我们有多个 snum 条目的 casenum。但我们只想选择从未有来自某个 snum 条目的 casenum
因此,例如,如果有一个 casenum 的条目仅来自 151 和 152,但没有来自 153 和 154 的条目,那么它将显示。
但如果 casenum 有来自 snum 153 或 154 的条目,则它不会显示它
解决方案
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
推荐阅读
- design-patterns - 为 Knuth Morris Pratt 算法寻找模式的问题
- ip - Akamai 阻止了我的 Comcast IP 地址 - 如何修复?
- python - 如何知道在框架内选择了哪个按钮并稍后在 tkinter 中使用其文本?
- python - 子进程在 Python 3.7+ 中包含有问题的引号
- javascript - 如何在 Bootstrap 4 中切换折叠图标的动画?
- svg.js - 如何绘制 45 度的 svg 折线?
- python - 为什么 pandas 25.3 版的 groupby 和 agg 不再起作用了?
- r - 反应性地过滤数据以生成地图
- sql - 向存储过程添加列是否有性能风险
- android - Apk 在 libs 中不包含 arm64-v8a 文件夹,但我的项目在 jnilibs/arm64-v8a/xyz.so 下有 .so 文件