0

我有一个查询:

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-零”替换它,我知道我知道如何在 SQL 中做到这一点

ISNULL(COUNT(*), 0)

但这不适用于 DB2,我该如何实现呢?您的所有帮助都非常感谢。

4

3 回答 3

2

将 COALESCE 包裹在 SELECT 列表中的每个可为空的总数周围:

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。这就是您想要的方式吗?查询工作?

于 2012-06-15T23:35:15.713 回答
0

而不是使用ISNULL(COUNT(*), 0),

尝试使用COALESCE(COUNT(*),0)

于 2012-06-15T21:27:26.787 回答
0

用于IFNULL(COUNT(*), 0)DB2

于 2013-03-15T12:56:45.103 回答