sql - SQL Server:获取其他列都具有相同值的列值
问题描述
在下面的示例中,我在获取所需内容时遇到问题。
我的数据:
Id AnotherId Status
-- --------- ------
1 3 new
2 3 old
3 5 new
4 6 new
5 11 old
6 11 old
7 55 new
8 55 new
我正在寻找AnotherId
每个 status = 'New' 实例的独特之处
所以我的结果看起来像:
5
6
55
这将使我大部分时间到达那里,但我需要所有“新”状态记录:
select AnotherId
from MyData
group by AnotherId
having count(distinct Status) = 1
我似乎无法得到我需要的东西。任何帮助,将不胜感激!
解决方案
您可以使用group by
子句:
select AnotherId
from table t
group by AnotherId
having min(status) = max(status) and min(status) = 'new';
推荐阅读
- python - WriteToText 仅写入临时文件
- java - 如何使用嵌套的 ArrayList 遍历 HashMap
- firefox - 在继续执行脚本之前等待应用程序关闭
- python-3.x - 如何在测试文件中导入 Flask 应用程序以进行单元测试
- wso2 - WSO2 API 管理器多租户分布式设置
- typescript - 防止 TypeScript 中的两种类型重叠
- julia - 罕见的,看似随机的分段错误和只读内存错误:为什么?我该如何解决?
- python - 我通过数据框的python循环随着时间的推移而减慢
- python - 使用 python 时来自 POSTMAN 的 GET 请求失败
- json - 如何在 Go 中动态处理 JSON 响应中的缺失字段