根据Oracle中的百分比计算更新多列

时间:2020-07-09 19:11:38

标签: oracle stored-procedures

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

我需要通过与其他表进行比较来更新某些列,并按百分比更新列。所以我走了。

首先,要获取 CMM批准长度的查询与下面我需要的其他列

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;

上面查询的输出如下图

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

现在是第二部分,它是计算百分比明智的比例

案例1:如果上图中CMM批准的长度为70km,而来自NE.MV_SPAN@DB_LINK_NE_VIEWER的(NE,UG,AR len)的NE长度为100Km,则除以(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

中更新

,表格样本数据如下所示:-

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

情况2:如果CMM approved lengthNE length相同。例如:70km(UG = 60 Km,AR = 10Km),则在TBL_FIBER_INV_SIGN_OFF_SHEET表中应更新UG = 60Km and AR = 10Km

下面是两个表的表说明。

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;

但是iam坚持计算百分比比例。

2 个答案:

答案 0 :(得分:1)

好的,所以经过大量的澄清之后,看起来应该是这样:

+更新:重复

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;

答案 1 :(得分:1)

根据您的问题解释,更新应该是

update TBL_FIBER_INV_CMP_REPORT_MV 
set UG =  CASE WHEN ROUTE_APPROVED_BY_CMM > 70 AND  NE_LENGTH >= 100 
        THEN NE_LENGTH*0.8 
      WHEN ROUTE_APPROVED_BY_CMM =  NE_LENGTH 
    THEN NE_LENGTH*(60/70)  
    ELSE 0 END

 , AR = CASE WHEN ROUTE_APPROVED_BY_CMM > 70 AND  NE_LENGTH >= 100 
        THEN NE_LENGTH*0.2 
      WHEN ROUTE_APPROVED_BY_CMM =  NE_LENGTH 
    THEN NE_LENGTH*(60/10) 
    ELSE 0 END