2013-10-08 34 views
0

我試圖僅顯示符合我的條件的值,這是基於出現次數的值。 假設如下表:顯示出現特定次數的值,特別是列

+--------------+----------------+ 
| account_id | campaign_id | 
+--------------+----------------+ 
|  234 |   980 | 
|  893 |   458 | 
|  234 |   178 | 
|  097 |   741 | 
|  893 |   584 | 
|  893 |   452 | 
|  109 |   789 | 
+--------------+----------------+

我需要證明account_ids僅具有兩個或兩個以上campaign_ids。因此,對於上述表中的結果應該是:使用count
234
893

where表達是不可能的。我用過:
CASE WHEN count(account_id) >= 2 THEN es.entity_name ELSE NULL
END AS Live_date

但是,它有這麼多記錄與我的查詢無關。我發現的大多數相關問題都是關於尋找出現次數而不是將其用作條件。有沒有其他的解決方案。

回答

1

請嘗試使用HAVING條款:

select 
    account_id 
From YourTable 
group by account_id 
having count(*)>=2 
1
select account_id 
from table 
group by account_id 
having count(*) >= 2