SQL 查询以查找所有记录的特定值 [英] SQL query to find a specific value for all records

查看:48
本文介绍了SQL 查询以查找所有记录的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Col1;Col2;Col3
12345;01;Y
12345;02;Y
12345;03;Y
22222;01;Y
22222;02;Y
22222;03;N
33333;01;N
44444;01;Y

需要帮助编写 SQL 查询以根据 col1 查找值 = 'Y' 的所有记录.对于 Eg 输出,选择 Col1 应给出输出为 12345 和 44444 [不是 22222 和 33333,因为 col3 包含 'N'为他们]

Need help in writing a SQL query to find all the records with value = 'Y' based on col1.For Eg output select Col1 should give the output as 12345 and 44444 [ not 22222 and 33333 as the col3 contains 'N' for them ]

非常感谢您的时间

推荐答案

我猜你需要 col1 其中 col3 的所有值都应该是 Y

I guess you need col1 where all values of col3 should be Y

select col1
from demo
group by col1
having count(*) = sum(Col3 = 'Y')

演示

或者,如果 col3 只能有 2 个可能的值,例如 Y/N,那么您可以将 have 子句简化为

Or if there can be only 2 possible values for col3 like Y/N then you can simplify your having clause as

having sum(Col3 = 'N') = 0

演示

这篇关于SQL 查询以查找所有记录的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆