2011-07-27 133 views
0

組我下面的查詢:甲骨文空值問題與

SELECT EMAIL.MAILADDRESS, COUNT (DISTINCT CUSTOMER.CUSTOMER_ID) distinct_count 
    FROM ( SELECT CUSTOMER_ID, 
         TRUNC (MAX (UPDATE_DATE)) AS UPDATE_TARIHI, 
         TRUNC (MIN (INSERT_DATE)) AS DATA_ENTRY_DATE, 
         'YES' AS MAILADDRESS 
       FROM ELECTRONIC_ADDRESS 
       WHERE ADDRESS_TYPE = 'ELECTRONIC' 
      GROUP BY CUSTOMER_ID) EMAIL 
     RIGHT OUTER JOIN 
      CUSTOMER 
     ON (EMAIL.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID) 
GROUP BY EMAIL.MAILADDRESS 

我加入電子郵件(子查詢-incoludes「E」 AS MAILADDRESS)和客戶表。 有些客戶不具有的emailadress所以我認爲結果應該是像belowe條款:

MAILADDRESS  DISTINCT_COUNT 
YES      158945 
NULL      76345 

但是當我運行此查詢時,只顯示第一行(其電子郵件地址是YES)

如果我將EMAIL.MAILADDRESS子句更改爲選定列中的nvl(EMAIL.MAILADDRESS,「否」)並逐個分組,查詢運行完美。 但我無法解決問題,因爲該查詢是從Business Objects Universe生成的,所以我無法更改該查詢。

我們寫這個部分:

SELECT CUSTOMER_ID, 
        TRUNC (MAX (UPDATE_DATE)) AS UPDATE_TARIHI, 
        TRUNC (MIN (INSERT_DATE)) AS DATA_ENTRY_DATE, 
        'YES' AS MAILADDRESS 
      FROM ELECTRONIC_ADDRESS 
      WHERE ADDRESS_TYPE = 'ELECTRONIC' 
     GROUP BY CUSTOMER_ID 

,然後構建一個合適的加入這個部分和客戶表之間的關係。

我該如何解決問題?爲什麼Oracle不顯示空值?

回答

0

你說你加入這個查詢

select customer_id 
     ,trunc(max(update_date)) as update_tarihi 
     ,trunc(min(insert_date)) as data_entry_date 
     ,'YES' as mailaddress 
    from electronic_address 
    where address_type = 'ELECTRONIC' 
    group by customer_id 

客戶表。這並不意味着你只會得到有電子郵件地址的記錄(='YES')?

+1

如果你看一下在第一個查詢後,你會看到這個部分在「右外連接」的左側。 – Allan

1

使用以下SQL,我無法重新創建您的問題。

CREATE TABLE a1 (a NUMBER); 

CREATE TABLE a2 (a NUMBER); 

INSERT INTO a1 
    SELECT  LEVEL 
    FROM  DUAL 
    CONNECT BY LEVEL <= 10; 

INSERT INTO a2 
    SELECT  LEVEL 
    FROM  DUAL 
    CONNECT BY LEVEL <= 20; 

SELECT a1.a, COUNT(DISTINCT a2.a) 
FROM  a1 RIGHT OUTER JOIN a2 ON a1.a = a2.a 
GROUP BY a1.a; 

這表明問題出在數據上,而不是SQL。如果客戶沒有電子郵件地址,那麼在ELECTRONIC_ADDRESS表中仍有記錄,但其他列是空的,是否有可能?

如果是這樣的話,我會如下改變你的子查詢(其中,「地址」是包含電子郵件地址的實際列):

SELECT CUSTOMER_ID, 
      TRUNC (MAX (UPDATE_DATE)) AS UPDATE_TARIHI, 
      TRUNC (MIN (INSERT_DATE)) AS DATA_ENTRY_DATE, 
      CASE WHEN ADDRESS IS NULL 
       THEN NULL 
       ELSE 'YES' 
       END AS MAILADDRESS 
    FROM ELECTRONIC_ADDRESS 
    WHERE ADDRESS_TYPE = 'ELECTRONIC' 
GROUP BY CUSTOMER_ID