在我的Oracle数据库中,我有一个左外连接,用于父工作订单到其子工作程序。然后我运行一个计算执行一些子值的SUM()。我将来自Nvl()中的子工作程序的结果包装起来,以确保它们能够正确计算。
当父级没有子工作符时,除了。在这种情况下,显示期间返回的“空值”是因为连接中没有结果,因此看起来Nvl([fieldname],0)不会将它们转换为显示0.因此,当我认为总结时具有父值的子值,由于向空值添加值,它们也返回null。
解决这个问题的最佳方法是什么?这是可以解决的问题,还是从根本上解决了我的查询有问题的气味?
抱歉,我暂时无法发布设置。对于此特定工作顺序(硬编码),连接的“右手”部分为空,因为父级没有子级,因此显示为空。
SELECT *
FROM (SELECT *
FROM R_PCR_ALLWOSANDTASKSSEPARATELY)WOINFO
LEFT OUTER JOIN (SELECT WORKORDERNUMBER AS TASKWORKORDRENUMBER
, PARENT AS TASKPARENT
, NVL(TOTALMATESTCOSTFORWO, 0) AS TOTALMATESTCOSTFORWO_TASK
, NVL(TOTALLABESTCOSTFORWO,0) AS TOTALLABESTCOSTFORWO_TASK
, NVL(TOTALMATACTCOSTFORWO,0) AS TOTALMATACTCOSTFORWO_TASK
, NVL(TOTALLABACTCOSTFORWO,0) AS TOTALLABACTCOSTFORWO_TASK
, NVL(TOTALLABACTHOURSFORWO,0) AS TOTALLABACTHOURSFORWO_TASK
FROM R_PCR_ALLWOSANDTASKSSEPARATELY)TASKINFO
ON ( WOINFO.WORKORDERNUMBER = TASKINFO.TASKPARENT )
WHERE WORKORDERNUMBER = '2826059';
答案 0 :(得分:6)
您需要在LEFT join返回结果后应用NVL。
试试这个:
SELECT WOINFO.*
, TASKWORKORDRENUMBER
, TASKPARENT
, NVL(TOTALMATESTCOSTFORWO_TASK,0) AS TOTALMATESTCOSTFORWO_TASK
, NVL(TOTALLABESTCOSTFORWO_TASK,0) AS TOTALLABESTCOSTFORWO_TASK
, NVL(TOTALMATACTCOSTFORWO_TASK,0) AS TOTALMATACTCOSTFORWO_TASK
, NVL(TOTALLABACTCOSTFORWO_TASK,0) AS TOTALLABACTCOSTFORWO_TASK
, NVL(TOTALLABACTHOURSFORWO_TASK,0) AS TOTALLABACTHOURSFORWO_TASK
FROM R_PCR_ALLWOSANDTASKSSEPARATELY WOINFO
LEFT OUTER JOIN (SELECT WORKORDERNUMBER AS TASKWORKORDRENUMBER
, PARENT AS TASKPARENT
, TOTALMATESTCOSTFORWO_TASK
, TOTALLABESTCOSTFORWO_TASK
, TOTALMATACTCOSTFORWO_TASK
, TOTALLABACTCOSTFORWO_TASK
, TOTALLABACTHOURSFORWO_TASK
FROM R_PCR_ALLWOSANDTASKSSEPARATELY) TASKINFO
ON ( WOINFO.WORKORDERNUMBER = TASKINFO.TASKPARENT )
WHERE WORKORDERNUMBER = '2826059';