 
 

ALTER VIEW [dbo].[View_HJGL_InstallData]
    AS
        WITH  TwOutPutData as (select distinct twRelation.PipelineId,
                                              outdetail.Id as TwOutputDetailId,
                                              twRelation.MaterialCode,
                                              master.Id    as OutputMasterId,
											  LEFT(planmaster.WeldTaskId, CHARINDEX('|', planmaster.WeldTaskId + '|') - 1) AS unitworkid
                              from Tw_OutputMaster master
                                       join Tw_OutputDetail outdetail on outdetail.OutputMasterId = master.Id
                                       join Tw_InOutPlanMaster planmaster on planmaster.Id = master.InOutPlanMasterId
                                       join Tw_InOutPlanDetail_Relation twRelation
                                            on twRelation.InOutPlanMasterId = planmaster.Id and
                                               outdetail.MaterialCode = twRelation.MaterialCode
                              where  master.TypeInt=70),
			 packdetailDate as ( 
							 select 
							 packdetail.Id,
							 packdetail.PipelineComponentId,
							 pack.TrainNumberId,
							 pack.PackagingCode,
							 pack.StackingPosition,
							 pack.ProjectId,
							 packdetail.MaterialCode,
							 packdetail.Number,
							 packdetail.TwOutputDetailId
							 from  HJGL_PackagingManageDetail packdetail
							 join  HJGL_PackagingManage pack ON packdetail.PackagingManageId = pack.PackagingManageId 
			 ),
             PrefabricatedData AS (SELECT pipe.PrefabricatedComponents           as Id,
                                          line.PipelineCode,
                                          pipe.PrefabricatedComponents as Code,
                                          '预制组件'                   as TypeStr,
                                          ''                           as Matdef,
                                          CAST(NULL AS DECIMAL(18, 2)) as Number, -- 明确指定数据类型
										  CAST(NULL AS DECIMAL(18, 2)) as ActNumber, -- 实到数量
                                          pack.PackagingCode,
                                          trainnumber.TrainNumber,
                                          line.FlowingSection,
                                          line.UnitWorkId,
                                          line.ProjectId,
                                          pack.StackingPosition
                                   FROM dbo.HJGL_PipeLineMat pipe
                                            INNER JOIN dbo.HJGL_Pipeline line -- 改为INNER JOIN，如果管道必须存在
                                                       ON pipe.PipelineId = line.PipelineId
                                            LEFT JOIN dbo.HJGL_MaterialCodeLib lib
                                                      ON lib.MaterialCode = pipe.MaterialCode
                                            LEFT JOIN HJGL_Pipeline_Component comonent
                                                      ON comonent.PipelineComponentCode = pipe.PrefabricatedComponents
                                            LEFT JOIN HJGL_PackagingManageDetail packdetail
                                                        ON packdetail.PipelineComponentId = comonent.PipelineComponentId
                                             LEFT JOIN HJGL_PackagingManage pack
                                                        ON packdetail.PackagingManageId = pack.PackagingManageId
                                                            AND pack.ProjectId = line.ProjectId -- 添加项目关联条件
                                              LEFT JOIN HJGL_TrainNumberManage trainnumber
                                                        ON pack.TrainNumberId = trainnumber.Id
                                   WHERE line.PipeArea = '1'
                                     and (pipe.PrefabricatedComponents != ''
                                       AND pipe.PrefabricatedComponents IS NOT NULL  and pipe.PrefabricatedComponents not in('裕-量'))),
             LooseComponentsData AS (SELECT distinct pipe.PipeLineMatId as Id,
                                                     line.PipelineCode,
                                                     pipe.MaterialCode  as Code,
                                                     '预制散件'         as TypeStr,
                                                     lib.MaterialDef    as Matdef,
                                                     cast( packdetail.Number as DECIMAL(18, 2)) as Number,
													 cast( packdetail.Number as DECIMAL(18, 2)) as ActNumber,
                                                     packdetail.PackagingCode,
                                                     trainnumber.TrainNumber,
                                                     line.FlowingSection,
                                                     line.UnitWorkId,
                                                     line.ProjectId,
                                                     packdetail.StackingPosition
                                     FROM dbo.HJGL_PipeLineMat pipe
                                              INNER JOIN HJGL_Pipeline line -- 改为INNER JOIN
                                                         ON pipe.PipelineId = line.PipelineId
                                              LEFT JOIN dbo.HJGL_MaterialCodeLib lib
                                                        ON lib.MaterialCode = pipe.MaterialCode
                                              LEFT JOIN TwOutPutData twOutPutData
                                                        ON twOutPutData.PipelineId = pipe.PipelineId and
                                                           twOutPutData.MaterialCode = pipe.MaterialCode and
														   twOutPutData.unitworkid =line.UnitWorkId 
                                              LEFT JOIN packdetailDate packdetail
                                                        ON packdetail.TwOutputDetailId = twOutPutData.TwOutputDetailId and packdetail.ProjectId= line.ProjectId  
                                              LEFT JOIN HJGL_TrainNumberManage trainnumber
                                                        ON packdetail.TrainNumberId = trainnumber.Id
                                     where line.PipeArea = '1'
                                       and (pipe.PrefabricatedComponents is null or pipe.PrefabricatedComponents = '') )

-- 合并结果
        SELECT distinct *
        FROM PrefabricatedData
        UNION ALL
        SELECT distinct *
        FROM LooseComponentsData




GO


