本文介绍了根据 Oracle 中的百分比计算更新多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:使用示例数据和输出更新总问题.

NOTE: Updating total question with sample data and output.

我需要通过与另一个表进行比较来更新一些列,并以百分比方式更新列.所以我走了.

I need to update some columns by comparing from another table and update the columns percentage wise. So here I go.

首先,获得CMM批准长度的查询在下面还有我需要的其他列

first of all , the query to get CMM approved length is below with other columns which I need

select CIRCLE,regexp_substr(MP,'[^/]+',1,1)MPNAME,regexp_substr(MP,'[^/]+',1,2)MPCODE,
 SPAN_TYPE,SPAN_LINK_ID,NE_LENGTH AS NE_LEN,
 ROUTE_APPROVED_BY_CMM as CMM_APPROVED_LENGTH from
 TBL_FIBER_INV_CMP_REPORT_MV
 where CMM_APPROVED_DATE IS NOT NULL OR ROUTE_APPROVED_BY_CMM > 0 OR
 JOB_PROGRESS_FLAG = 1;

上面查询的输出如下图所示

and the output of above query looks like this below image

[![在此处输入图片描述][1]][1]

[![enter image description here][1]][1]

现在是第二部分,即百分比的计算

CASE 1:如果上图中 CMM 批准的长度是 70km 并且从 (NE,UG, AR len from NE.MV_SPAN@DB_LINK_NE_VIEWER) 出来的 NE 长度为 100Km 然后划分 (UG=80Km 和 AR=20Km)

CASE 1: If The CMM approved length in above image is 70km and NE length from (NE,UG, AR len from NE.MV_SPAN@DB_LINK_NE_VIEWER)comes out to be 100Km then divide (UG=80Km and AR=20Km)

那么分叉的百分比将是 80% UG_length 和 20% AR_length.因此,针对 CMM 批准的 70 公里公里,TBL_FIBER_INV_SIGN_OFF_SHEET 应将列更新为 UG = 56KmAR = 14Km.分叉应在下表中更新TBL_FIBER_INV_SIGN_OFF_SHEET

Then percentage of bifurcation would be 80% UG_length and 20% AR_length. So, against 70km of CMM approved kilometer, TBL_FIBER_INV_SIGN_OFF_SHEET should update the column as UG = 56Km and AR = 14Km.the bifurcation should be updated in below table TBL_FIBER_INV_SIGN_OFF_SHEET

表示例数据如下所示:-

and the table sample data looks like below:-

[![在此处输入图片描述][2]][2]

[![enter image description here][2]][2]

CASE 2: 如果CMM 批准长度NE 长度 相同.例如:70km(UG = 60 Km 和 AR=10Km)然后在 TBL_FIBER_INV_SIGN_OFF_SHEET 表中应该更新 UG = 60Km and AR = 10Km.

CASE 2: IF CMM approved length and NE length is same. for ex: 70km (UG = 60 Km and AR=10Km) then in TBL_FIBER_INV_SIGN_OFF_SHEET table should update UG = 60Km and AR = 10Km.

以下是两个表的表描述.

Below are the table description of both the tables.

CREATE OR REPLACE PROCEDURE UPD_UG_AR_BY_CMM AS
   BEGIN


    for cur_r in (
    select circle,
                   regexp_substr(MP,'[^/]+',1,1)MAINTENANCE_ZONE_NAME,
                   regexp_substr(MP,'[^/]+',1,2)MAINTENANCE_ZONE_CODE,
                   SPAN_TYPE,
                   SPAN_LINK_ID,
                   NE_LENGTH,
                   ROUTE_APPROVED_BY_CMM
                   from TBL_FIBER_INV_CMP_REPORT_MV
                  where CMM_APPROVED_DATE IS NOT NULL
                  OR ROUTE_APPROVED_BY_CMM > 0
                  OR JOB_PROGRESS_FLAG = 1
        )

  LOOP

        IF cur_r.ROUTE_APPROVED_BY_CMM >  SELECT ROUND(SUM(NVL(CALCULATED_LENGTH,0)/1000),4) AS NE_LENGTH,
                                           ROUND(SUM(CASE WHEN RJ_CONSTRUCTION_METHODOLOGY NOT LIKE '%AERIAL%' OR RJ_CONSTRUCTION_METHODOLOGY IS  NULL THEN NVL(CALCULATED_LENGTH,0) ELSE 0 END)/1000,4) AS UG_LENGTH
                                           ,ROUND(SUM(CASE WHEN RJ_CONSTRUCTION_METHODOLOGY LIKE '%AERIAL%' THEN NVL(CALCULATED_LENGTH,0) ELSE 0 END)/1000,4) AS AR_LENGTH
                                           FROM NE.MV_SPAN@DB_LINK_NE_VIEWER
        THEN

          BEGIN
              UPDATE TBL_FIBER_INV_SIGN_OFF_SHEET
              SET  FSA_UG = UG_LENGTH,  --- divide 80% of NE_LENGTH
                   FSA_AERIAL = AR_LENGTH    --- divide 20% of NE_LENGTH
              WHERE CUR_R.SPAN_LINK_ID =  RJ_SPAN_ID

          END



    NULL;
  END UPD_UG_AR_BY_CMM;

但我一直在计算百分比明智的比率.

but iam stuck at calculation of percentage wise ratio.

推荐答案

ok,所以最后经过大量的澄清,看起来应该是:

ok, so finally after tons of clarifications, looks like it should be:

+更新:重复

merge into TBL_FIBER_INV_SIGN_OFF_SHEET DST
using (
   -- Source: original query
   with mv as (
       select * from (
          select
             circle,
             regexp_substr(MP,'[^/]+',1,1)MAINTENANCE_ZONE_NAME,
             regexp_substr(MP,'[^/]+',1,2)MAINTENANCE_ZONE_CODE,
             SPAN_TYPE,
             SPAN_LINK_ID,
             NE_LENGTH, -- ? NE_LENGTH - 1
             ROUTE_APPROVED_BY_CMM as CMM_APPROVED_LENGTH,
             row_number()over(partition by SPAN_LINK_ID order by ROUTE_APPROVED_BY_CMM) rn
          from TBL_FIBER_INV_CMP_REPORT_MV
          where CMM_APPROVED_DATE IS NOT NULL
             OR ROUTE_APPROVED_BY_CMM > 0
             OR JOB_PROGRESS_FLAG = 1
       )
       where rn=1
   )
   ,pct as (
       SELECT
          ROUND(SUM(NVL(CALCULATED_LENGTH,0)/1000),4) AS NE_LENGTH -- ? NE_LENGTH - 2
         ,ROUND(SUM(CASE WHEN RJ_CONSTRUCTION_METHODOLOGY NOT LIKE '%AERIAL%'
                         OR RJ_CONSTRUCTION_METHODOLOGY IS NULL
                         THEN NVL(CALCULATED_LENGTH,0)
                         ELSE 0
                    END)/1000,4
               ) AS UG_LENGTH
         ,ROUND(SUM(CASE WHEN RJ_CONSTRUCTION_METHODOLOGY LIKE '%AERIAL%'
                         THEN NVL(CALCULATED_LENGTH,0)
                         ELSE 0
                    END)/1000,4
               ) AS AR_LENGTH
          FROM NE.MV_SPAN@DB_LINK_NE_VIEWER
   )
   select
      mv.*
     --,cpt.*
     ,case when mv.CMM_APPROVED_LENGTH > pct.NE_LENGTH then 0.8 * pct.UG_length
           when mv.CMM_APPROVED_LENGTH = pct.NE_LENGTH then 0.9 * pct.UG_length
      end as FSA_UG
     ,case when mv.CMM_APPROVED_LENGTH > pct.NE_LENGTH then 0.2 * pct.AR_LENGTH
           when mv.CMM_APPROVED_LENGTH = pct.NE_LENGTH then 0.1 * pct.AR_LENGTH
      end as FSA_AERIAL
   from mv, pct
   -- end of Source query
) SRC
on ( dst.SPAN_LINK_ID =  src.SPAN_LINK_ID )
when matched then update
   set FSA_UG = src.FSA_UG
      ,FSA_AERIAL = src.FSA_AERIAL;

这篇关于根据 Oracle 中的百分比计算更新多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:59