本文介绍了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 查询以查找所有记录的特定值
发布评论