select * from (SELECT xt.productLineId,xt.productLine,gd.SFB05,gd.PName FROM S_SY_ProductLine as xt left join sfb_file as gd on xt.productLineId = gd.SFBUD10 where FactoryID = 'Y20' and gd.SFB02 = '1' and SFB82 = 'Y20' )as bbb cross apply (select top 5 DP from FR_DepartProcessCFG where DP is not null and sort >=1 order by sort asc )as ccc cross apply (SELECT Dates,直通率,最新出料数,weekdate FROM [BMMES_JM].[dbo].[Assist_DateAndWeek] as a cross apply( select a.Dates as rq,convert( varchar,convert( decimal(18,7),EXP(SUM(LOG((ll)))))) as 直通率,cl.最新出料数 from( SELECT RpAcceptNo,(case when 良品数 = 0 then null else 良品数 end) as 良品数,(case when 报工数 = 0 then null else 报工数 end) as 报工数 ,convert( varchar,convert( decimal(18,5),((良品数/报工数))))as 良率,(case when convert( decimal(18,7),((良品数/报工数))) = 0 then null else convert( decimal(18,7),((良品数/报工数))) end) as ll FROM ( SELECT RpAcceptNo,cast(sum(RpAcceptCount) as decimal(18,2)) as 良品数,cast(sum(RpTotalNums) as decimal(18,2)) as 报工数 FROM [BMMES_JM].[dbo].[M_JOB_REPORTWORK_01] lpsj left join [BMMES_JM].[dbo].[M_JOB_REPORTWORK] bgsj on lpsj.RpLPID = bgsj.RpID where RpAuditStatus = '1' AND RpAcceptNo in( SELECT ProcessID FROM [BMMES_JM].[dbo].[FR_DepartProcessCFG] where DPM = ccc.DP) and RpProduceLine = bbb.productLineId and RpFinishID = bbb.SFB05 and RpAuditDate between convert(varchar(10),DATEADD(dd,0,a.Dates),120)+' 07:00' and convert(varchar(10),DATEADD(dd,1,a.Dates),120)+' 07:00' AND RpABMaterial = 'A' and CHARINDEX('_',RpERPWorkOrder)=0 GROUP BY RpAcceptNo )as bgsjz ) as ztl CROSS APPLY( SELECT (case when 出料良品数 is null then 0 else 出料良品数 end)-(case when 退料良品数 is null then 0 else 退料良品数 end) as 最新出料数 FROM ( SELECT ProcessID ,sum(OpmAmount) as 出料良品数 FROM [BMMES_JM].[dbo].[P_PM_OutputMaterial] where ProcessID =(select processID from [BMMES_JM].[dbo].[FR_DepartProcessCFG] where sort >0 and DP = ccc.DP) and OpmQuality = '良品' and OpmProductNum = bbb.SFB05 and OpmLineID = bbb.productLineId and OpmAuditDate between convert(varchar(10),DATEADD(dd,0,a.Dates),120)+' 07:00' and convert(varchar(10),DATEADD(dd,1,a.Dates),120)+' 07:00' group by ProcessID)as cllp left join( SELECT RA_ToProc,SUM(RA_ApplyNums) as 退料良品数 FROM [BMMES_JM].[dbo].[P_PM_RetreatApply] where RA_ApplyPosi = '待产仓' and RA_ApplyPN = bbb.SFB05 and RA_ToProc LIKE (select processID from [BMMES_JM].[dbo].[FR_DepartProcessCFG] where sort >0 and DP = ccc.DP) and RA_Quality = '良品' and RA_TOProductLineID = bbb.productLineId and RA_AduitDate between convert(varchar(10),DATEADD(dd,0,a.Dates),120)+' 07:00' and convert(varchar(10),DATEADD(dd,1,a.Dates),120)+' 07:00'and RA_AduitStatus = '1' GROUP BY RA_ToProc )as tllp on tllp.RA_ToProc = cllp.ProcessID )as cl group by cl.最新出料数 )as new where a.Dates like '2019%' and weeks = '3' )as aaa left join (select weekData,TargetQty,DeliveryQty,jhs.PlanId,ProcessesID,LineCode,MaterielCode from [BMMES_JM].[dbo].[M_FactoryProduce_PlanDetail]as mb left join [BMMES_JM].[dbo].[M_FactoryProduce_PlanHead] as jhs on mb.Planid = jhs.PlanId left join [BMMES_JM].[dbo].[sfb_file] as gds on jhs.LineCode = gds.SFBUD10 where jhs.PlanId is not null and ProcessesID = (select ProcessID from FR_DepartProcessCFG where DP = aaa.dp and sort>0) and weekData = 'WK3' and gds.SFB02 = '1' and SFB82 = 'Y20' and LineCode = aaa.productLineId and MaterielCode = (select distinct SFB05 from sfb_file where Pname = aaa.PName) )as mbs on aaa.weekdate = mbs.weekData