ok, så äntligen efter massor av förtydliganden, ser det ut som det borde vara:
+uppdatering:dubbletter
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 [email protected]_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;