2013-02-08 34 views
0

我有一個大表,但對這個問題的目的,讓我們假設我都有以下列strucure:WHERE語句W /鮮明

enter image description here

我想有一個Where語句返回只有電子郵件地址在該特定列中不同的行。

想法?

回答

2

在大多數數據庫,你可以做到這一點

select t.AccountId, t.BillingEmail 
from (select t.*, count(*) over (partition by BillingEmail) as cnt 
     from t 
    ) t 
where cnt = 1 

這種方法的好處是,當你從上表喜歡,你可以得到儘可能多的列。

3
SELECT BillingEMail 
FROM tableName 
GROUP BY BillingEMail 
HAVING COUNT(BillingEMail) = 1 

OR HAVING COUNT(*) = 1

我不知道是什麼RDBMS使用的是(爲什麼我不能用引入分析功能的原因),但你可以通過加入子查詢來做到這一點,如果你想獲得所有列

SELECT a.* 
FROM tableName a 
     INNER JOIN 
     (
     SELECT BillingEMail 
     FROM tableName 
     GROUP BY BillingEMail 
     HAVING COUNT(BillingEMail) = 1 
    )b ON a.BillingEMail = b.BillingEMail 
0

我更喜歡JW的方法,但這裏是另一個使用NOT EXISTS的方法。

SELECT AccountID, [Billing Email] 
FROM table t1 
WHERE NOT EXISTS (
    -- Make sure that no other row contains the same 
    -- email, but a different Account ID. 
    SELECT 1 
    FROM table t2 
    WHERE t1.[Billing Email] = t2.[Billing Email] 
    AND t1.AccountID <> t2.AccountID 
)