我有一个问题:
SELECT A.AHSHMT AS SHIPMENT, A.AHVNAM AS VENDOR_NAME, D.UNITS_SHIPPED, D.ADPON AS PO, B.NUMBER_OF_CASES_ON_TRANSIT, C.NUMBER_OF_CASES_RECEIVED FROM AHASNF00 A
INNER JOIN (SELECT IDSHMT, COUNT(*) AS NUMBER_OF_CASES_ON_TRANSIT FROM IDCASE00 WHERE IDSTAT = '01' GROUP BY IDSHMT) B
ON (A.AHSHMT = B.IDSHMT)
LEFT JOIN (SELECT IDSHMT, (COUNT(*) AS NUMBER_OF_CASES_RECEIVED FROM IDCASE00 WHERE IDSTAT = '10' GROUP BY IDSHMT) C
ON (A.AHSHMT = C.IDSHMT)
INNER JOIN (SELECT ADSHMT, ADPON, SUM(ADUNSH) AS UNITS_SHIPPED FROM ADASNF00 GROUP BY ADSHMT, ADPON) D
ON (A.AHSHMT = D.ADSHMT)
WHERE A.AHSHMT = '540041134';
在第一个JOIN语句中,我有一个COUNT(*),在此计数中,有时我会得到NULL。我需要将其替换为“0-zero”,我知道我知道如何在SQL中执行此操作
ISNULL(COUNT(*), 0)
但是这对DB2不起作用,我该如何实现呢?你所有的帮助都非常感激。
答案 0 :(得分:2)
在SELECT列表中的每个可空总计周围包裹COALESCE:
SELECT A.AHSHMT AS SHIPMENT,
A.AHVNAM AS VENDOR_NAME,
COALESCE( D.UNITS_SHIPPED, 0 ) AS UNITS_SHIPPED,
D.ADPON AS PO,
COALESCE( B.NUMBER_OF_CASES_ON_TRANSIT, 0 ) AS NUMBER_OF_CASES_ON_TRANSIT,
COALESCE( C.NUMBER_OF_CASES_RECEIVED, 0 ) AS NUMBER_OF_CASES_RECEIVED
FROM ...
您用于表达式B和D的内部联接意味着您将只接收来自A的行,这些行具有一个或多个传输中的案例(表达式B)并且在表达式D中具有一个或多个PO。是这样的您希望您的查询有效吗?
答案 1 :(得分:0)
而不是使用ISNULL(COUNT(*), 0)
,
尝试使用COALESCE(COUNT(*),0)
答案 2 :(得分:0)
对DB2使用IFNULL(COUNT(*), 0)