﻿
ALTER VIEW [dbo].[View_Report_DocumentationStatusMC]
AS
/*********DocumentationStatusMC报告*************/
SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,
  (SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D01_IFC' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='01_Process' ORDER BY ReceiveHC DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='30_Plumbing/F.F' ORDER BY ReceiveHC DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='40_Mech./Equi.' ORDER BY ReceiveHC DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='50_Piping' ORDER BY ReceiveHC DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='60_Electrical' ORDER BY ReceiveHC DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='65_Telecommunication' ORDER BY ReceiveHC DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='70_Instrument' ORDER BY ReceiveHC DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='10_Civil' ORDER BY ReceiveHC DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='20_Architectral' ORDER BY ReceiveHC DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='20_HVAC' ORDER BY ReceiveHC DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='10_MasterpLanning' ORDER BY ReceiveHC DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='02_HSE' ORDER BY ReceiveHC DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='00_ALL' ORDER BY ReceiveHC DESC) AS ALLS

FROM dbo.Editor_EProject eproject

UNION ALL

SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,
	   (SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D02_AS-BUILT' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='01_Process' ORDER BY ReceiveHC DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='30_Plumbing/F.F' ORDER BY ReceiveHC DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='40_Mech./Equi.' ORDER BY ReceiveHC DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='50_Piping' ORDER BY ReceiveHC DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='60_Electrical' ORDER BY ReceiveHC DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='65_Telecommunication' ORDER BY ReceiveHC DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='70_Instrument' ORDER BY ReceiveHC DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='10_Civil' ORDER BY ReceiveHC DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='20_Architectral' ORDER BY ReceiveHC DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='20_HVAC' ORDER BY ReceiveHC DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='10_MasterpLanning' ORDER BY ReceiveHC DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='02_HSE' ORDER BY ReceiveHC DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='00_ALL' ORDER BY ReceiveHC DESC) AS ALLS

FROM dbo.Editor_EProject eproject

UNION ALL

SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,
(SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D03_MD' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='01_Process' ORDER BY ReceivePDF DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='30_Plumbing/F.F' ORDER BY ReceivePDF DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='40_Mech./Equi.' ORDER BY ReceivePDF DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='50_Piping' ORDER BY ReceivePDF DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='60_Electrical' ORDER BY ReceivePDF DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='65_Telecommunication' ORDER BY ReceivePDF DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='70_Instrument' ORDER BY ReceivePDF DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='10_Civil' ORDER BY ReceivePDF DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='20_Architectral' ORDER BY ReceivePDF DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='20_HVAC' ORDER BY ReceivePDF DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='10_MasterpLanning' ORDER BY ReceivePDF DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='02_HSE' ORDER BY ReceivePDF DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='00_ALL' ORDER BY ReceivePDF DESC) AS ALLS

FROM dbo.Editor_EProject eproject



GO



ALTER VIEW [dbo].[View_Report_DocumentationStatusMC_OUT]
AS
/*********DocumentationStatusMC报告*************/
SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,eProject.PM_General_CDI,eProject.OutSourceType,
	  (CASE WHEN CONVERT(VARCHAR(10), eproject.ProjectControl_MS_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_Approval, 23) ELSE '' END) AS ApprSch,
      (CASE WHEN CONVERT(VARCHAR(10), eproject.PM_MA_ProjectApproval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_ProjectApproval, 23) ELSE '' END) AS ApprAct,
 (SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D01_IFC' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='01_Process' ORDER BY ReceiveHC DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='30_Plumbing/F.F' ORDER BY ReceiveHC DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='40_Mech./Equi.' ORDER BY ReceiveHC DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='50_Piping' ORDER BY ReceiveHC DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='60_Electrical' ORDER BY ReceiveHC DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='65_Telecommunication' ORDER BY ReceiveHC DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='70_Instrument' ORDER BY ReceiveHC DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='10_Civil' ORDER BY ReceiveHC DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='20_Architectral' ORDER BY ReceiveHC DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='20_HVAC' ORDER BY ReceiveHC DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='10_MasterpLanning' ORDER BY ReceiveHC DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='02_HSE' ORDER BY ReceiveHC DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='00_ALL' ORDER BY ReceiveHC DESC) AS ALLS,

-----------------------------------------------
--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%P10%' OR p.Roles LIKE '%P11%' OR p.Roles LIKE '%P12%' OR p.Roles LIKE '%P13%')
--FOR XML PATH('')),1,1,'') AS P1_Process, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%P30%' OR p.Roles LIKE '%P31%' OR p.Roles LIKE '%P32%' OR p.Roles LIKE '%P33%')
--FOR XML PATH('')),1,1,'') AS P3_Plumbing_FF, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%P20%' OR p.Roles LIKE '%P21%' OR p.Roles LIKE '%P22%' OR p.Roles LIKE '%P23%')
--FOR XML PATH('')),1,1,'') AS P2_Mech_Equi,

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%P40%' OR p.Roles LIKE '%P41%' OR p.Roles LIKE '%P42%' OR p.Roles LIKE '%P43%')
--FOR XML PATH('')),1,1,'') AS P4_Piping,  

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%A20%' OR p.Roles LIKE '%A21%' OR p.Roles LIKE '%A22%' OR p.Roles LIKE '%A23%')
--FOR XML PATH('')),1,1,'') AS A2_Electrical, 
--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%A30%' OR p.Roles LIKE '%A31%' OR p.Roles LIKE '%A32%' OR p.Roles LIKE '%A33%')
--FOR XML PATH('')),1,1,'') AS A3_Telec, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%A10%' OR p.Roles LIKE '%A11%' OR p.Roles LIKE '%A12%' OR p.Roles LIKE '%A13%')
--FOR XML PATH('')),1,1,'') AS A1_Instrument, 


--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%C10%' OR p.Roles LIKE '%C11%' OR p.Roles LIKE '%C12%' OR p.Roles LIKE '%C13%')
--FOR XML PATH('')),1,1,'') AS C1_Civil, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%C30%' OR p.Roles LIKE '%C31%' OR p.Roles LIKE '%C32%' OR p.Roles LIKE '%C33%')
--FOR XML PATH('')),1,1,'') AS C3_Architectral, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%C40%' OR p.Roles LIKE '%C41%' OR p.Roles LIKE '%C42%' OR p.Roles LIKE '%C43%')
--FOR XML PATH('')),1,1,'') AS C4_HVAC, 

--STUFF((SELECT ','+u.UserName FROM ManHours_Plan AS p 
--		LEFT JOIN Sys_User AS u ON u.UserId = p.EngineerId 
--		WHERE p.EProjectId = eproject.EProjectId 
--		AND (p.Roles LIKE '%C20%' OR p.Roles LIKE '%C21%' OR p.Roles LIKE '%C22%' OR p.Roles LIKE '%C23%')
--FOR XML PATH('')),1,1,'') AS C2_MasterPlanning, 

pu.UserName AS P1_Process,
'' AS P3_Plumbing_FF,
meu.UserName AS P2_Mech_Equi,
pu.UserName AS P4_Piping,  
eleu.UserName AS A2_Electrical, 
eleu.UserName AS A3_Telec, 
insu.UserName AS A1_Instrument, 
civu.UserName AS C1_Civil, 
'' AS C3_Architectral,
'' AS C4_HVAC,
'' AS C2_MasterPlanning, 
-----------------------------------------------

 s.Detail_Eng_Civil_Sch_Start,--PROJ_DE_CIVIL_SCH_START_DATE	
 s.Detail_Eng_Civil_Sch_End,--PROJ_DE_CIVIL_SCH_END_DATE	
 s.PROJ_DE_CIVIL_ACU_START_DATE,--PROJ_DE_CIVIL_ACU_START_DATE
 s.PROJ_DE_CIVIL_ACU_END_DATE,--PROJ_DE_CIVIL_ACU_END_DATE
 s.Detail_Eng_Mech_EI_Sch_Start,--PROJ_DE_ME_SCH_START_DATE
 s.PROJ_DE_ME_SCH_END_DATE,--PROJ_DE_ME_SCH_END_DATE	
 s.Detail_Eng_Mech_EI_Sch_End,--PROJ_DE_ME_ACU_START_DATE	
 s.PROJ_DE_ME_ACU_END_DATE --PROJ_DE_ME_ACU_END_DATE
FROM dbo.Editor_EProject eproject
LEFT JOIN dbo.Sys_User pu ON eproject.ProjectControl_CI_Process=pu.UserId
LEFT JOIN dbo.Sys_User meu ON eproject.ProjectControl_CI_Equipment=meu.UserId
LEFT JOIN dbo.Sys_User eleu ON eproject.ProjectControl_CI_Electrical=eleu.UserId
LEFT JOIN dbo.Sys_User insu ON eproject.ProjectControl_CI_Instrument=insu.UserId
LEFT JOIN dbo.Sys_User civu ON eproject.ProjectControl_CI_Civil=civu.UserId
LEFT JOIN dbo.View_ScheduleDate s ON s.EProjectId = eproject.EProjectId

UNION ALL

SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,eProject.PM_General_CDI,eProject.OutSourceType,
	  (CASE WHEN CONVERT(VARCHAR(10), eproject.ProjectControl_MS_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_Approval, 23) ELSE '' END) AS ApprSch,
      (CASE WHEN CONVERT(VARCHAR(10), eproject.PM_MA_ProjectApproval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_ProjectApproval, 23) ELSE '' END) AS ApprAct,
	  (SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D02_AS-BUILT' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='01_Process' ORDER BY ReceiveHC DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='30_Plumbing/F.F' ORDER BY ReceiveHC DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='40_Mech./Equi.' ORDER BY ReceiveHC DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='50_Piping' ORDER BY ReceiveHC DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='60_Electrical' ORDER BY ReceiveHC DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='65_Telecommunication' ORDER BY ReceiveHC DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='70_Instrument' ORDER BY ReceiveHC DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='10_Civil' ORDER BY ReceiveHC DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='20_Architectral' ORDER BY ReceiveHC DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='20_HVAC' ORDER BY ReceiveHC DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='10_MasterpLanning' ORDER BY ReceiveHC DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='02_HSE' ORDER BY ReceiveHC DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D02_AS-BUILT' AND Disc='00_ALL' ORDER BY ReceiveHC DESC) AS ALLS,

-----------------------------------------------
pu.UserName AS P1_Process,
'' AS P3_Plumbing_FF,
meu.UserName AS P2_Mech_Equi,
pu.UserName AS P4_Piping,  
eleu.UserName AS A2_Electrical, 
eleu.UserName AS A3_Telec, 
insu.UserName AS A1_Instrument, 
civu.UserName AS C1_Civil, 
'' AS C3_Architectral,
'' AS C4_HVAC,
'' AS C2_MasterPlanning, 
-----------------------------------------------
 s.Detail_Eng_Civil_Sch_Start,--PROJ_DE_CIVIL_SCH_START_DATE	
 s.Detail_Eng_Civil_Sch_End,--PROJ_DE_CIVIL_SCH_END_DATE	
 s.PROJ_DE_CIVIL_ACU_START_DATE,--PROJ_DE_CIVIL_ACU_START_DATE
 s.PROJ_DE_CIVIL_ACU_END_DATE,--PROJ_DE_CIVIL_ACU_END_DATE
 s.Detail_Eng_Mech_EI_Sch_Start,--PROJ_DE_ME_SCH_START_DATE
 s.PROJ_DE_ME_SCH_END_DATE,--PROJ_DE_ME_SCH_END_DATE	
 s.Detail_Eng_Mech_EI_Sch_End,--PROJ_DE_ME_ACU_START_DATE	
 s.PROJ_DE_ME_ACU_END_DATE --PROJ_DE_ME_ACU_END_DATE

FROM dbo.Editor_EProject eproject
LEFT JOIN dbo.Sys_User pu ON eproject.ProjectControl_CI_Process=pu.UserId
LEFT JOIN dbo.Sys_User meu ON eproject.ProjectControl_CI_Equipment=meu.UserId
LEFT JOIN dbo.Sys_User eleu ON eproject.ProjectControl_CI_Electrical=eleu.UserId
LEFT JOIN dbo.Sys_User insu ON eproject.ProjectControl_CI_Instrument=insu.UserId
LEFT JOIN dbo.Sys_User civu ON eproject.ProjectControl_CI_Civil=civu.UserId
LEFT JOIN dbo.View_ScheduleDate s ON s.EProjectId = eproject.EProjectId

UNION ALL

SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,eProject.PM_General_CDI,eProject.OutSourceType,
	  (CASE WHEN CONVERT(VARCHAR(10), eproject.ProjectControl_MS_Approval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), ProjectControl_MS_Approval, 23) ELSE '' END) AS ApprSch,
      (CASE WHEN CONVERT(VARCHAR(10), eproject.PM_MA_ProjectApproval, 23) IS NOT NULL THEN CONVERT(VARCHAR(10), PM_MA_ProjectApproval, 23) ELSE '' END) AS ApprAct,
       (SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D03_MD' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='01_Process' ORDER BY ReceivePDF DESC) AS Process,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='30_Plumbing/F.F' ORDER BY ReceivePDF DESC) AS Plumbling,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='40_Mech./Equi.' ORDER BY ReceivePDF DESC) AS Equipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='50_Piping' ORDER BY ReceivePDF DESC) AS Piping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='60_Electrical' ORDER BY ReceivePDF DESC) AS Electrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='65_Telecommunication' ORDER BY ReceivePDF DESC) AS Communication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='70_Instrument' ORDER BY ReceivePDF DESC) AS Instrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='10_Civil' ORDER BY ReceivePDF DESC) AS Civil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='20_Architectral' ORDER BY ReceivePDF DESC) AS Architectral,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='20_HVAC' ORDER BY ReceivePDF DESC) AS HVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='10_MasterpLanning' ORDER BY ReceivePDF DESC) AS MasterpLanning,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='02_HSE' ORDER BY ReceivePDF DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceivePDF,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D03_MD' AND Disc='00_ALL' ORDER BY ReceivePDF DESC) AS ALLS,

-----------------------------------------------
pu.UserName AS P1_Process,
'' AS P3_Plumbing_FF,
meu.UserName AS P2_Mech_Equi,
pu.UserName AS P4_Piping,  
eleu.UserName AS A2_Electrical, 
eleu.UserName AS A3_Telec, 
insu.UserName AS A1_Instrument, 
civu.UserName AS C1_Civil, 
'' AS C3_Architectral,
'' AS C4_HVAC,
'' AS C2_MasterPlanning, 
-----------------------------------------------
 s.Detail_Eng_Civil_Sch_Start,--PROJ_DE_CIVIL_SCH_START_DATE	
 s.Detail_Eng_Civil_Sch_End,--PROJ_DE_CIVIL_SCH_END_DATE	
 s.PROJ_DE_CIVIL_ACU_START_DATE,--PROJ_DE_CIVIL_ACU_START_DATE
 s.PROJ_DE_CIVIL_ACU_END_DATE,--PROJ_DE_CIVIL_ACU_END_DATE
 s.Detail_Eng_Mech_EI_Sch_Start,--PROJ_DE_ME_SCH_START_DATE
 s.PROJ_DE_ME_SCH_END_DATE,--PROJ_DE_ME_SCH_END_DATE	
 s.Detail_Eng_Mech_EI_Sch_End,--PROJ_DE_ME_ACU_START_DATE	
 s.PROJ_DE_ME_ACU_END_DATE --PROJ_DE_ME_ACU_END_DATE

FROM dbo.Editor_EProject eproject
LEFT JOIN dbo.Sys_User pu ON eproject.ProjectControl_CI_Process=pu.UserId
LEFT JOIN dbo.Sys_User meu ON eproject.ProjectControl_CI_Equipment=meu.UserId
LEFT JOIN dbo.Sys_User eleu ON eproject.ProjectControl_CI_Electrical=eleu.UserId
LEFT JOIN dbo.Sys_User insu ON eproject.ProjectControl_CI_Instrument=insu.UserId
LEFT JOIN dbo.Sys_User civu ON eproject.ProjectControl_CI_Civil=civu.UserId
LEFT JOIN dbo.View_ScheduleDate s ON s.EProjectId = eproject.EProjectId

GO




ALTER VIEW [dbo].[View_Report_DocumentationStatusIFC]
AS
/*********DocumentationStatusIFC报告*************/
SELECT eproject.EProjectId,eproject.ProjectControl_JobNo,
       eproject.ProjectControl_ProjectManager,eproject.ProjectControl_LeadByName,
       eproject.ProjectControl_BUCode,CONVERT(NVARCHAR(10),eproject.CM_MA_MC,23) AS MC,
       eproject.ProjectControl_JobStatus,eproject.ProjectControl_JobType,
	   eproject.PM_MA_JobReveive,
(SELECT TOP 1 UserName FROM dbo.Sys_User WHERE UserId=(SELECT TOP 1 EngineerId FROM dbo.ManHours_Plan WHERE EProjectId=eproject.EProjectId AND Roles='D10')) AS ProjectControl_TDCManager,
 'D01_IFC' AS Phase,
(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Process') AS PlanProcess,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='01_Process' ORDER BY ReceiveHC DESC) AS Process,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Plumbing/F.F') AS PlanPlumbing,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='30_Plumbing/F.F' ORDER BY ReceiveHC DESC) AS Plumbling,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Mech./Equi.') AS PlanEquipment,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='40_Mech./Equi.' ORDER BY ReceiveHC DESC) AS Equipment,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Piping') AS PlanPiping,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='50_Piping' ORDER BY ReceiveHC DESC) AS Piping,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Electrical') AS PlanElectrical,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='60_Electrical' ORDER BY ReceiveHC DESC) AS Electrical,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Telecommunication') AS PlanTelecommunication,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='65_Telecommunication' ORDER BY ReceiveHC DESC) AS Communication,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Instrument') AS PlanInstrument,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='70_Instrument' ORDER BY ReceiveHC DESC) AS Instrument,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Civil') AS PlanCivil,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='10_Civil' ORDER BY ReceiveHC DESC) AS CivilStruc,

(SELECT TOP 1 CONVERT(NVARCHAR(10),SchEnd,23) FROM dbo.Editor_PM WHERE EProjectId=eproject.EProjectId AND DisciplinesWBSName ='Architectral') AS PlanHVAC,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='20_Architectral' ORDER BY ReceiveHC DESC) AS ArchHVAC,

(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='02_HSE' ORDER BY ReceiveHC DESC) AS HSE,
(SELECT TOP 1 CONVERT(NVARCHAR(10),ReceiveHC,23) FROM dbo.Editor_TDC WHERE EProjectId=eproject.EProjectId AND [Type]='D01_IFC' AND Disc='00_ALL' ORDER BY ReceiveHC DESC) AS ALLS

FROM dbo.Editor_EProject eproject


GO




