求和NULL或将NULL转换为0以求和sql db2.iseries求和

时间:2015-02-26 16:14:41

标签: sql db2

我是SQL的新手,我目前正在尝试查询DB2.iSeries数据库,而且我被卡住了。

这是我的代码:

SELECT IPROD, IDESC, IMRP, NONAV
FROM

(SELECT IPROD, IDESC, IMRP FROM IIM WHERE IBUYC IN (<pln.value>) AND IMRP = 'N') AS IM

INNER JOIN
(SELECT I01PROD FROM INV01P
WHERE I01SUPS = '0'
AND I01SUPP = '0')
AS L1 ON IM.IPROD = L1.I01PROD

LEFT OUTER JOIN
(SELECT WPROD, SUM(WOPB-WISS+WADJ+WRCT) AS NONAV FROM IWI
WHERE LEFT(WWHS,1) = '9'
GROUP BY WPROD) AS L2 ON IM.IPROD = L2.WPROD

GROUP BY IPROD, IDESC, IMRP, NONAV
HAVING SUM(NONAV) = 0

ORDER BY IPROD
FETCH FIRST 10000 ROWS ONLY

这是我在查询时工作正常的代码:HAVING SUM(NONAV) = 0,但我遇到的问题是有些产品的NULL值,我还需要提取。

我在查询的不同位置尝试了IFNULLCASE WHEN,但它失败了。

我知道如何将字段输出视图设为0,但是当涉及到总和时,它无法提取这些产品。

一旦字段汇总,有人可以帮我拉出NULL和0值。

由于

1 个答案:

答案 0 :(得分:0)

SELECT IPROD, IDESC, IMRP, NONAV
FROM

(SELECT IPROD, IDESC, IMRP FROM IIM WHERE IBUYC IN (<pln.value>) AND IMRP = 'N') AS IM

INNER JOIN
(SELECT I01PROD FROM INV01P
WHERE I01SUPS = '0'
AND I01SUPP = '0')
AS L1 ON IM.IPROD = L1.I01PROD

LEFT OUTER JOIN
(SELECT WPROD, SUM(WOPB-WISS+WADJ+WRCT) AS NONAV FROM IWI
WHERE LEFT(WWHS,1) = '9'
GROUP BY WPROD) AS L2 ON IM.IPROD = L2.WPROD

GROUP BY IPROD, IDESC, IMRP, NONAV
HAVING SUM(COALESCE(NONAV,0)) = 0

ORDER BY IPROD
FETCH FIRST 10000 ROWS ONLY