![](/img/trans.png)
[英]How to return only distinct results within one column, while allowing duplicates in other columns?
[英]Fetching a distinct column only if duplicates columns have the same data
我有這種類型的表:
+-----TABLE1----+
| Name, Boolean |
| A , 0 |
| B , 0 |
| B , 0 |
| A , 1 |
| C , 1 |
| D , 0 |
| C , 0 |
| A , 0 |
| A , 1 |
| B , 0 |
| D , 0 |
+--------------+
我想 select 區分所有重復布爾值也為 0 的名稱。 所以結果將是:
+---------------+
| RESULT: |
| Name |
| B |
| D |
+---------------+
因為 A 和 C 包含 boolean 也是“1”,所以它們不會被提取
我可以:
SELECT DISTINCT name, MAX(boolean) as boolean FROM table1 GROUP BY name;
但是,如果我只想獲取它們的 max(boolean) 為 0 的結果,我需要使用什么條件?
我不能在一個查詢中使用兩個 select 語句,因為我們正在談論大數據數據庫。所以在我的情況下,這個解決方案不是一個選項:
SELECT DISTINCT t1.name FROM table1 t WHERE t1.name NOT IN (SELECT DISTINCT t2.name FROM table1 t2 WHERE t2.boolean = 1);
在使用“JOIN”時也想不出一個選項。
有什么解決辦法嗎?
提前致謝: :)
您可以使用聚合:
select name
from table1 t
group by name
having min(boolean) = max(boolean);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.