SQL 查询以查找所有记录的特定值

编程入门 行业动态 更新时间:2024-10-26 15:18:40
本文介绍了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 查询以查找所有记录的特定值

本文发布于:2023-10-23 19:43:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定值   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!