如果为null,则返回一个值

时间:2016-12-22 15:07:34

标签: oracle null zero

在oracle中,我有一段代码可以返回我们的工单通过率。它基本上取失败次数(MRB)并除以处理的工作订单数(WO)来给出费率。我遇到的问题是,除非出现故障(MRB),否则我将无法获得相应月份的退货。如果没有MRB失败,我希望该月的回报率为100%。这是我正在使用的代码。为了记录,我不是程序员,我一路上只是收集了一些信息。我试过添加NVL,但似乎没有帮助。

detectMSERFeatures

以上代码返回以下内容:

Select To_Char(MTH, 'Month') As "Month",WO, MRB,
    Round(1 - (MRB / WO), 3) *      100 As "Pass Rate",
    '98' As Goal
 From 
 (
        Select Trunc(g.START_DATE, 'Month') As mth,
                Count(Distinct V_PDAYPROD_CRW1.PDAYPROD_ID) As WO,
                Count(Distinct V_WF_HEADER_MRB.ID) As MRB
        From GLPERIODS g
        Left Join V_PDAYPROD_CRW1 On Trunc(g.START_DATE, 'Month') = Trunc(V_PDAYPROD_CRW1.PROD_DATE, 'Month')
        Left Join V_WF_HEADER_MRB On Trunc(g.START_DATE, 'Month') = Trunc(V_WF_HEADER_MRB.OPEN_DATE, 'Month')
        Inner Join ARINVT On V_PDAYPROD_CRW1.ARINVT_ID = ARINVT.ID
        Where Extract(Year From g.START_DATE) = Extract(Year From SysDate) 
          And V_WF_HEADER_MRB.WF_TYPE_ID = '99' 
          And V_WF_HEADER_MRB.EPLANT_ID = 2 
          And ARINVT.EPLANT_ID = 2
        Group By Trunc(g.START_DATE, 'Month'),
             V_WF_HEADER_MRB.WF_TYPE_ID
)
Group By To_Char(MTH, 'Month'),WO,MRB,
   Round(1 - (MRB / WO), 3) * 100,
   '98',MTH
Order By MTH

我希望它能返回这样的东西:

  MONTH     |  WO   |  MRB  |  Pass Rate  | GOAL 
  September |  60   |  1    |    98.3     |  98 
  December  |  30   |  2    |    93.3     |  98

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:-1)

如果您希望在结果集中返回主表中的所有记录,则不应在" v_wf_header_mrb "上使用过滤条件。 where子句中的表。

删除" V_WF_HEADER_MRB .WF_TYPE_ID =' 99' V_WF_HEADER_MRB .EPLANT_ID = 2"条件来自Where子句并尝试。

您可以改用以下查询。

SELECT
    TO_CHAR(mth,'Month') AS "Month",
    wo,
    mrb,
    round(1 - (mrb / wo),3) * 100 AS "Pass Rate",
    '98' AS goal
FROM
    (
        SELECT
            trunc(g.start_date,'Month') AS mth,
            COUNT(DISTINCT v_pdayprod_crw1.pdayprod_id) AS wo,
            COUNT(DISTINCT v_wf_header_mrb.id) AS mrb
        FROM
            glperiods g
        LEFT JOIN
            v_pdayprod_crw1
        ON
            trunc(g.start_date,'Month') = trunc(v_pdayprod_crw1.prod_date,'Month')
        LEFT JOIN
            v_wf_header_mrb
        ON
            trunc(g.start_date,'Month') = trunc(v_wf_header_mrb.open_date,'Month')
        AND
            v_wf_header_mrb.wf_type_id = '99'
        AND
            v_wf_header_mrb.eplant_id = 2
            INNER JOIN
                arinvt
            ON
                v_pdayprod_crw1.arinvt_id = arinvt.id
        WHERE
            EXTRACT(YEAR FROM g.start_date) = EXTRACT(YEAR FROM SYSDATE)
        AND
            arinvt.eplant_id = 2
        GROUP BY
            trunc(g.start_date,'Month'),
            v_wf_header_mrb.wf_type_id
    )
GROUP BY
    TO_CHAR(mth,'Month'),
    wo,
    mrb,
    round(1 - (mrb / wo),3) * 100,
    '98',
    mth
ORDER BY mth;