如果DB2查询中没有结果,我试图显示“0”。 Coalesce不适合我。这是DB2 LUW v8.2。我怎么能这样做?
SELECT 'Stuff',
COALESCE((SUM(OI.QUANTITY)),0)
FROM DB2ADMIN.ORDERS O INNER JOIN DB2ADMIN.ORDERITEMS OI ON O.ORDERS_ID = OI.ORDERS_ID
WHERE DATE(O.TIMEPLACED) = (DATE(CURRENT DATE) - 1 DAYS)
AND OI.CATENTRY_ID = 000001
GROUP BY DATE(O.TIMEPLACED)
答案 0 :(得分:0)
您可以使用Common Table Expression执行此操作。
WITH MYCTE (MYSTUFF, MYSUM) AS (
SELECT 'Stuff',0
FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 'Stuff', SUM(OI.QUANTITY)
FROM DB2ADMIN.ORDERS O INNER JOIN DB2ADMIN.ORDERITEMS OI ON O.ORDERS_ID = OI.ORDERS_ID
WHERE DATE(O.TIMEPLACED) = (DATE(CURRENT DATE) - 1 DAYS)
AND OI.CATENTRY_ID = 000001
GROUP BY DATE(O.TIMEPLACED))
SELECT MYSTUFF, SUM(MYSUM)
FROM MYCTE
GROUP BY MYSTUFF;
答案 1 :(得分:0)
可能Coalesce正在工作,但总和因为null而失败,你是否尝试在SUM中使用COALESCE或NVL?即
SELECT 'Stuff',
(SUM(COALESCE(OI.QUANTITY,0)))
FROM DB2ADMIN.ORDERS O INNER JOIN DB2ADMIN.ORDERITEMS OI ON O.ORDERS_ID = OI.ORDERS_ID
WHERE DATE(O.TIMEPLACED) = (DATE(CURRENT DATE) - 1 DAYS)
AND OI.CATENTRY_ID = 000001
GROUP BY DATE(O.TIMEPLACED)