我有以下查询:
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
我正在加入电子邮件(subquery-incoludes'E'AS MAILADDRESS)和客户表。 有些客户没有电子邮件地址,所以我认为结果应该像下面的条款:
MAILADDRESS DISTINCT_COUNT
YES 158945
NULL 76345
但是当我运行此查询时,只显示第一行(其邮件地址为YES)
如果我将EMAIL.MAILADDRESS子句更改为所选列中的nvl(EMAIL.MAILADDRESS,'NO')并按部分分组,则查询将完美运行。 但我无法解决问题,因为该查询是从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 :(得分: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
答案 1 :(得分: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')?