2014-02-07 40 views
1

我使用下面的查詢基於如下3個條件從表中獲得唯一的記錄計數:如何使用表格上的oneColumn和Union上的Distinct獲取所有列?

Select COUNT(*) FROM ( select distinct(SUBSCRIBER_EMAIL) from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '30000' and '31999' 
    Union 
select distinct(SUBSCRIBER_EMAIL) from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '39813' and '39901' 
    Union 
select distinct(SUBSCRIBER_EMAIL) from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '32000' and '34999' 
) Counts 

現在我想的是:我想要得到的這些記錄的所有數據..

我已經試過

Select distinct(SUBSCRIBER_EMAIL), SUBSCRIBER_Id FROM (select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '30000' and '31999' 
    Union  
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '39813' and '39901'  
    Union 
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '32000' and '34999'  
) x 

但使用此我得到的所有記錄,意味着截然不同的是不是在這種情況下工作。

請幫助我如何獲得與其他數據不同的電子郵件地址。

回答

0

爲什麼不去做不同的是這樣的:

SELECT distinct * from 
(
    Select distinct(SUBSCRIBER_EMAIL), SUBSCRIBER_Id FROM (select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '30000' and '31999' 
    Union  
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '39813' and '39901'  
    Union 
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '32000' and '34999'  
) 
+0

沒有我已經試過這個,它不工作.. –

+0

你是否得到任何錯誤,或者它只是不返回你想要的? – vlio20

0

首先查詢可以是這樣的:

Select COUNT(*) FROM ( 
select distinct(SUBSCRIBER_EMAIL) 
from SUBSCRIBER 
where SUBSCRIBER_ACTIVE = 1 
and (SUBSCRIBER_ZIP between '32000' and '34999' 
    OR SUBSCRIBER_ZIP between '30000' and '31999' 
    OR SUBSCRIBER_ZIP between '39813' and '39901') 
) Counts 

其次......如果你想要的所有數據:

SELECT S.* 
FROM SUBSCRIBER S 
JOIN ( 
select distinct(SUBSCRIBER_EMAIL) 
from SUBSCRIBER 
where SUBSCRIBER_ACTIVE = 1 
and (SUBSCRIBER_ZIP between '32000' and '34999' 
    OR SUBSCRIBER_ZIP between '30000' and '31999' 
    OR SUBSCRIBER_ZIP between '39813' and '39901') 
) Counts 
ON S.SUBSCRIBER_EMAIL = Counts.SUBSCRIBER_EMAIL 
相關問題