219 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Transact-SQL
		
	
	
	
			
		
		
	
	
			219 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			Transact-SQL
		
	
	
	
 | 
						||
CREATE TABLE [dbo].[WBS_MilePost](
 | 
						||
	[MilePostId] [nvarchar](50) NOT NULL,
 | 
						||
	[ProjectId] [nvarchar](50) NULL,
 | 
						||
	[CnProfessionId] [int] NULL,
 | 
						||
	[MilePostName] [nvarchar](100) NULL,
 | 
						||
	[PlanDate] [datetime] NULL,
 | 
						||
	[RealDate] [datetime] NULL,
 | 
						||
	[CompileMan] [nvarchar](50) NULL,
 | 
						||
	[CompileDate] [datetime] NULL,
 | 
						||
	[Remark] [nvarchar](200) NULL,
 | 
						||
 CONSTRAINT [PK_WBS_MilePost] PRIMARY KEY CLUSTERED 
 | 
						||
(
 | 
						||
	[MilePostId] ASC
 | 
						||
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 | 
						||
) ON [PRIMARY]
 | 
						||
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost]  WITH CHECK ADD  CONSTRAINT [FK_WBS_MilePost_Base_Project] FOREIGN KEY([ProjectId])
 | 
						||
REFERENCES [dbo].[Base_Project] ([ProjectId])
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost] CHECK CONSTRAINT [FK_WBS_MilePost_Base_Project]
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost]  WITH CHECK ADD  CONSTRAINT [FK_WBS_MilePost_Sys_User] FOREIGN KEY([CompileMan])
 | 
						||
REFERENCES [dbo].[Sys_User] ([UserId])
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost] CHECK CONSTRAINT [FK_WBS_MilePost_Sys_User]
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost]  WITH CHECK ADD  CONSTRAINT [FK_WBS_MilePost_WBS_CnProfessionInit] FOREIGN KEY([CnProfessionId])
 | 
						||
REFERENCES [dbo].[WBS_CnProfessionInit] ([CnProfessionId])
 | 
						||
GO
 | 
						||
 | 
						||
ALTER TABLE [dbo].[WBS_MilePost] CHECK CONSTRAINT [FK_WBS_MilePost_WBS_CnProfessionInit]
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ö÷¼ü' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'MilePostId'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ÏîÄ¿Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'ProjectId'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'רҵId' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'CnProfessionId'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Àï³Ì±®Ãû³Æ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'MilePostName'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'¼Æ»®Íê³ÉÈÕÆÚ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'PlanDate'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ʵ¼ÊÍê³ÉÈÕÆÚ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'RealDate'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'±àÖÆÈË' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'CompileMan'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'±àÖÆÈÕÆÚ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'CompileDate'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'±¸×¢' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost', @level2type=N'COLUMN',@level2name=N'Remark'
 | 
						||
GO
 | 
						||
 | 
						||
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Àï³Ì±®±í' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'WBS_MilePost'
 | 
						||
GO
 | 
						||
 | 
						||
 | 
						||
 | 
						||
alter table [dbo].[WBS_CostControlDetail] add StartDate datetime null
 | 
						||
alter table [dbo].[WBS_CostControlDetail] add EndDate datetime null
 | 
						||
alter table [dbo].[WBS_CostControlDetailHistory] add StartDate datetime null
 | 
						||
alter table [dbo].[WBS_CostControlDetailHistory] add EndDate datetime null
 | 
						||
GO
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
alter table [dbo].[WBS_CostControlParentDetail] add StartDate datetime null
 | 
						||
alter table [dbo].[WBS_CostControlParentDetail] add EndDate datetime null
 | 
						||
GO
 | 
						||
 | 
						||
 | 
						||
ALTER VIEW [dbo].[View_WBS_CostControlParentDetail]
 | 
						||
AS
 | 
						||
/********¿ØÖÆÏ¼¶Ã÷ϸ********/
 | 
						||
SELECT detail.CostControlParentDetailId, 
 | 
						||
detail.ParentId, 
 | 
						||
detail.Months, 
 | 
						||
detail.StartDate,
 | 
						||
detail.EndDate,
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisPlanValue,0)) from WBS_CostControlParentDetail 
 | 
						||
where ParentId=detail.ParentId and Months=detail.Months)) as ThisPlanValue,    --±¾Ô¼ƻ®Íê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.ThisRealCost,0)) from WBS_CostControlParentDetail d
 | 
						||
where d.ParentId=detail.ParentId and Months=detail.Months)) as ThisRealCost,  --±¾ÔÂʵ¼Ê³É±¾ 
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisPlanCost,0)) from WBS_CostControlParentDetail 
 | 
						||
where ParentId=detail.ParentId and Months=detail.Months)) as ThisPlanCost,  --±¾ÔÂÍê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisPlanValue,0)) from WBS_CostControlParentDetail 
 | 
						||
where ParentId=detail.ParentId and Months<=detail.Months)) as TotalPlanValue,  --ÀۼƼƻ®Íê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.ThisRealCost,0)) from WBS_CostControlParentDetail d
 | 
						||
where d.ParentId=detail.ParentId and Months<=detail.Months)) as TotalRealCost,  --ÀÛ¼ÆÍê³É³É±¾
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisPlanCost,0)) from WBS_CostControlParentDetail 
 | 
						||
where ParentId=detail.ParentId and Months<=detail.Months)) as TotalPlanCost  --ÀÛ¼ÆÍê³ÉÔ¤Ëã
 | 
						||
FROM dbo.WBS_CostControlParentDetail AS detail
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
GO
 | 
						||
 | 
						||
 | 
						||
 | 
						||
ALTER VIEW [dbo].[View_WBS_CostControlDetail]
 | 
						||
AS
 | 
						||
/********¿ØÖÆÏîÃ÷ϸ********/
 | 
						||
SELECT detail.CostControlDetailId, 
 | 
						||
detail.CostControlId, 
 | 
						||
detail.Months, 
 | 
						||
detail.StartDate,
 | 
						||
detail.EndDate,
 | 
						||
c.CostControlCode, 
 | 
						||
c.CostControlName,
 | 
						||
c.WbsSetId, 
 | 
						||
c.ProjectId,
 | 
						||
c.Unit,   --µ¥Î»
 | 
						||
c.TotalNum,   --ºÏͬ¹¤×÷Á¿
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(PlanNum,0)) from WBS_CostControlDetail where CostControlId=detail.CostControlId and Months<=detail.Months and StartDate is null)) as TotalPlanNum,  --ÀۼƼƻ®Íê³ÉÁ¿
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from WBS_CostControlDetail where CostControlId=detail.CostControlId and Months<=detail.Months)) as TotalThisNum,  --ÀÛ¼ÆÍê³ÉÁ¿
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.PlanNum,0)) from WBS_CostControlDetail d
 | 
						||
 where d.CostControlId=detail.CostControlId and Months=detail.Months and StartDate is null)) as PlanNum,  --±¾Ô¼ƻ®Íê³ÉÁ¿
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.ThisNum,0)) from WBS_CostControlDetail d
 | 
						||
 where d.CostControlId=detail.CostControlId and Months=detail.Months)) as ThisNum,  --±¾ÔÂÍê³ÉÁ¿
 | 
						||
c.RealPrice,    --³É±¾µ¥¼Û
 | 
						||
c.PlanPrice,   --¿ØÖÆÔ¤Ëãµ¥¼Û
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.PlanNum,0)*ISNULL(c1.PlanPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
 where d.CostControlId=detail.CostControlId and Months=detail.Months and StartDate is null)) as ThisPlanValue,  --±¾Ô¼ƻ®Íê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.ThisNum,0)*ISNULL(c1.RealPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
 where d.CostControlId=detail.CostControlId and Months=detail.Months)) as ThisRealCost,  --±¾ÔÂʵ¼Ê³É±¾
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)*ISNULL(c1.PlanPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
where d.CostControlId=detail.CostControlId and Months=detail.Months)) as ThisPlanCost,  --±¾ÔÂÍê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.PlanNum,0)*ISNULL(c1.PlanPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
where d.CostControlId=detail.CostControlId and Months<=detail.Months and StartDate is null)) as TotalPlanValue,  --ÀۼƼƻ®Íê³ÉÔ¤Ëã
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(d.ThisNum,0)*ISNULL(c1.RealPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
where d.CostControlId=detail.CostControlId and Months<=detail.Months)) as TotalRealCost,  --ÀÛ¼ÆÍê³É³É±¾
 | 
						||
CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)*ISNULL(c1.PlanPrice,0)) from WBS_CostControlDetail d
 | 
						||
LEFT JOIN WBS_CostControl AS c1 ON c1.CostControlId=d.CostControlId
 | 
						||
where d.CostControlId=detail.CostControlId and Months<=detail.Months)) as TotalPlanCost  --ÀÛ¼ÆÍê³ÉÔ¤Ëã
 | 
						||
FROM dbo.WBS_CostControlDetail AS detail
 | 
						||
LEFT JOIN WBS_CostControl AS c ON c.CostControlId=detail.CostControlId
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
GO
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
ALTER VIEW [dbo].[View_WBS_CostControlDetailStatistics]
 | 
						||
AS
 | 
						||
select InstallationId as Id,null as SupId ,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 ,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,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,UnitProjectName as Name,'UnitProject' as WBSType,
 | 
						||
(case when u.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName=(select InstallationName 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, WbsSetName as Name,'WbsSet' as WBSType,
 | 
						||
(case when w.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName=(select InstallationName 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, WbsSetName as Name,'WbsSet' as WBSType,
 | 
						||
(case when w.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName=(select InstallationName 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,c.CostControlName as Name,'CostControl' as WBSType,
 | 
						||
(case when ws.CnProfessionId is null then (select CnProfessionId from WBS_CnProfessionInit where CnProfessionName=(select InstallationName 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.TotalNum is not null 
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
GO
 | 
						||
 | 
						||
 |