[英]SQL select the only rows with only a certain values in them
我在這里有同樣的問題: SQL選擇其中僅具有特定值的行
但是我發現的解決方案僅適用於給定的一個值,我的意思是我希望它適用於多個值{2或+},例如:
Col 1 Col 2 Col 3
1 A 1
2 A 2
3 B 1
4 C 1
5 C 2
6 D 1
7 C 3
我想獲取僅包含cols3 = 1和cols3 = 2(僅值1和2)的所有行,並且輸出將是:給定表的“ A”,我不想擁有C,因為它具有另一個值列3的!== 1或2,鏈接中的解決方案允許僅針對一個值執行此操作,但我想將其擴展為2值或加號,有人可以幫助我嗎:D
從表WHERE中選擇col 2(col 3 = 1或col 3 = 2)並且不輸入col 2(從表WHERE中選擇col 2(col 3 <> 2和col 3 <> 1));
您可以為此使用conditional aggregation
:
select col2
from yourtable
group by col2
having sum(col3=1) > 0
and sum(col3=2) > 0
and sum(col3 not in (1,2)) = 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.