
ALTER VIEW [dbo].[View_WBS_CostControlDetailStatistics]
AS
select InstallationId as Id,null as SupId ,InstallationCode as Code, InstallationName as Name,'Installation' as WBSType,null as OldCnProfessionId,null as OldUnitProjectCode,null as OldWbsSetCode,
ProjectId from dbo.Project_Installation where SuperInstallationId is null
Union
select InstallationId as Id,SuperInstallationId as SupId ,InstallationCode as Code, '['+InstallationCode+']'+InstallationName as Name,'Installation' as WBSType,null as OldCnProfessionId,null as OldUnitProjectCode,null as OldWbsSetCode,
ProjectId from dbo.Project_Installation where SuperInstallationId is not null
Union
select CnProfessionId as Id , InstallationId as SupId,CnProfessionCode as Code, CnProfessionName as Name,'CnProfession' as WBSType,OldId as OldCnProfessionId,null as OldUnitProjectCode,null as OldWbsSetCode,
ProjectId from dbo.WBS_CnProfession where IsApprove=1
Union all
select UnitProjectId as Id, isnull(CnProfessionId,InstallationId) as SupId,UnitProjectCode as Code,UnitProjectName as Name,'UnitProject' as WBSType,
(case when u.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName like (select '%'+substring(InstallationName,0,2)+'%' from Project_Installation where InstallationId=u.InstallationId)) else (select OldId from WBS_CnProfession c where c.CnProfessionId=u.CnProfessionId) end) as OldCnProfessionId,UnitProjectCode as OldUnitProjectCode,null as OldWbsSetCode, 
ProjectId from dbo.Wbs_UnitProject u where IsApprove=1
Union all
select WbsSetId as id,UnitProjectId as SupId,WbsSetCode as Code, WbsSetName as Name,'WbsSet' as WBSType,
(case when w.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName like (select '%'+substring(InstallationName,0,2)+'%' from Project_Installation where InstallationId=w.InstallationId)) else (select OldId from WBS_CnProfession c where c.CnProfessionId=w.CnProfessionId) end) as OldCnProfessionId,(select UnitProjectCode from Wbs_UnitProject c where c.UnitProjectId=w.UnitProjectId) as OldUnitProjectCode,
WbsSetCode as OldWbsSetCode,
ProjectId from dbo.Wbs_WbsSet w where IsApprove=1 and SuperWbsSetId is null
Union all
select WbsSetId as id,SuperWbsSetId as SupId,WbsSetCode as Code, WbsSetName as Name,'WbsSet' as WBSType,
(case when w.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName like (select '%'+substring(InstallationName,0,2)+'%' from Project_Installation where InstallationId=w.InstallationId)) else (select OldId from WBS_CnProfession c where c.CnProfessionId=w.CnProfessionId) end) as OldCnProfessionId,(select UnitProjectCode from Wbs_UnitProject c where c.UnitProjectId=w.UnitProjectId) as OldUnitProjectCode,
(select WbsSetCode from Wbs_WbsSet ws where ws.WbsSetId=w.SuperWbsSetId) as OldWbsSetCode,
ProjectId from dbo.Wbs_WbsSet w where IsApprove=1 and SuperWbsSetId is not null
Union all
select c.CostControlId as id,c.WbsSetId as SupId,CostControlCode as Code,c.CostControlName as Name,'CostControl' as WBSType,
(case when ws.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName like (select '%'+substring(InstallationName,0,2)+'%' from Project_Installation where InstallationId=ws.InstallationId)) else (select OldId from WBS_CnProfession cn where cn.CnProfessionId=(select top 1 CnProfessionId from Wbs_WbsSet w where w.WbsSetId=c.WbsSetId)) end) as OldCnProfessionId,
(select UnitProjectCode from Wbs_UnitProject u where u.UnitProjectId=(select top 1  UnitProjectId from Wbs_WbsSet w where w.WbsSetId=c.WbsSetId)) as OldUnitProjectCode,
(select WbsSetCode from Wbs_WbsSet u where u.WbsSetId=(select top 1 w.SuperWbsSetId from Wbs_WbsSet w where w.WbsSetId=c.WbsSetId)) as OldWbsSetCode,
c.ProjectId from dbo.WBS_CostControl c 
left join dbo.Wbs_WbsSet ws on ws.WbsSetId=c.WbsSetId
where c.IsSelected=1












GO


