CREATE PROCEDURE [dbo].[Sp_HJGL_ProductionPlanStatistics] @projectId nvarchar(50)=null AS select distinct unitWork.UnitWorkId, unitWork.UnitWorkCode, unitWork.UnitWorkName, unitWork.ProjectId, pipeline.FlowingSection, isnull(total.TotalDia,0) as TotalDia,--工厂预制总达因 p.PlanStartDate,--计划开始日期 p.PlanEndDate,--计划完成日期 p.Days as TotalDays,--总天数 (case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else '' end) as RemainingDays,--剩余天数 (case when p.Days>0 then cast(isnull(total.TotalDia,0)/p.Days as decimal(18,2)) else 0 end) as AvgDayCompleteDia, --平均每日应完成工作量 ISNULL(currentDay.CurrentDayCompletedDia,0) as CurrentDayCompletedDia, --当日完成工作量 cast(case when (case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else 0 end)>0 then (isnull(total.TotalDia,0)-isnull(totalCompleted.totalCompletedDia,0))/ (case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else 0 end) else 0 end as decimal(18,2)) as NextDayComplete, --次日应完成量 isnull(totalCompleted.totalCompletedDia,0) as totalCompletedDia, --累计已完成量 cast(cast((case when isnull(total.TotalDia,0)>0 then (isnull(totalCompleted.totalCompletedDia,0) / isnull(total.TotalDia,0)*100) else 0 end) as decimal(18,2)) as varchar(10))+'%' as CompletedRate, --已完成百分比 welder.WelderCount, --当前焊工数量 warningWelder.WarningWelderCount --预警焊工数量 FROM WBS_UnitWork AS unitWork LEFT JOIN (select FlowingSection,UnitWorkId from HJGL_Pipeline where PipeArea='1' and FlowingSection is not null and FlowingSection!='') as pipeline on pipeline.UnitWorkId = unitWork.UnitWorkId left join HJGL_ProductionSchedulingPlan p on p.PipelineId = unitWork.UnitWorkId and p.FlowNum = pipeline.FlowingSection --工厂预制总达因 left join (select sum(Size) as TotalDia,HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection from HJGL_WeldJoint left join HJGL_Pipeline on HJGL_Pipeline.PipelineId = HJGL_WeldJoint.PipelineId where HJGL_Pipeline.PipeArea='1' and HJGL_WeldJoint.JointAttribute='预制口' group by HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection) as total on total.UnitWorkId = unitWork.UnitWorkId and total.FlowingSection = pipeline.FlowingSection --当日完成工作量 left join (select sum(Size) as CurrentDayCompletedDia,HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection from HJGL_WeldJoint left join HJGL_Pipeline on HJGL_Pipeline.PipelineId = HJGL_WeldJoint.PipelineId left join HJGL_WeldingDaily on HJGL_WeldingDaily.WeldingDailyId = HJGL_WeldJoint.WeldingDailyId where HJGL_Pipeline.PipeArea='1' and HJGL_WeldJoint.JointAttribute='预制口' and HJGL_WeldJoint.WeldingDailyId is not null and YEAR(HJGL_WeldingDaily.WeldingDate)=YEAR(GETDATE()) and MONTH(HJGL_WeldingDaily.WeldingDate)=MONTH(GETDATE()) and DAY(HJGL_WeldingDaily.WeldingDate)=DAY(GETDATE()) group by HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection) as currentDay on currentDay.UnitWorkId = unitWork.UnitWorkId and currentDay.FlowingSection = pipeline.FlowingSection --累计已完成量 left join (select sum(Size) as totalCompletedDia,HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection from HJGL_WeldJoint left join HJGL_Pipeline on HJGL_Pipeline.PipelineId = HJGL_WeldJoint.PipelineId where HJGL_Pipeline.PipeArea='1' and HJGL_WeldJoint.JointAttribute='预制口' and HJGL_WeldJoint.WeldingDailyId is not null group by HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection) as totalCompleted on totalCompleted.UnitWorkId = unitWork.UnitWorkId and totalCompleted.FlowingSection = pipeline.FlowingSection --当前焊工数量 left join (select count(*) as WelderCount,HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection from Person_Persons left join HJGL_WeldJoint on HJGL_WeldJoint.BackingWelderId = Person_Persons.PersonId left join HJGL_Pipeline on HJGL_Pipeline.PipelineId = HJGL_WeldJoint.PipelineId where HJGL_Pipeline.PipeArea='1' and HJGL_WeldJoint.JointAttribute='预制口' and HJGL_WeldJoint.WeldingDailyId is not null group by HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection) as welder on welder.UnitWorkId = unitWork.UnitWorkId and welder.FlowingSection = pipeline.FlowingSection --预警焊工数量 left join (select count(*) as WarningWelderCount,HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection from Person_Persons left join HJGL_WeldJoint on HJGL_WeldJoint.BackingWelderId = Person_Persons.PersonId left join HJGL_Pipeline on HJGL_Pipeline.PipelineId = HJGL_WeldJoint.PipelineId where HJGL_Pipeline.PipeArea='1' and HJGL_WeldJoint.JointAttribute='预制口' and HJGL_WeldJoint.WeldingDailyId is not null and PersonId in (select WelderId from Welder_WelderQualify where LimitDate < GETDATE()) group by HJGL_Pipeline.UnitWorkId,HJGL_Pipeline.FlowingSection) as warningWelder on warningWelder.UnitWorkId = unitWork.UnitWorkId and warningWelder.FlowingSection = pipeline.FlowingSection where unitWork.ProjectId = @projectId and pipeline.FlowingSection is not null