alter table JDGL_QuantityCompletion add StartDate datetime null
alter table JDGL_QuantityCompletion add EndDate datetime null
GO


ALTER VIEW [dbo].[View_JDGL_QuantityCompletion]
AS
select qc.*,ql.DesignNum,ql.Name,ql.Unit,
                            CONVERT(FLOAT,(select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate())) as TotalPlanNum,
                            CONVERT(FLOAT,(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate())) as TotalRealNum,
                            CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(qc.PlanNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL(qc.RealNum,0) / (1.0 *qc.PlanNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate,
		                    CONVERT(NVARCHAR(10),((CAST(CASE (select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate()) WHEN 0 THEN 0
		ELSE 100*(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate()) / (1.0 *(select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate())) END AS DECIMAL(9,2)))))+'%'
		AS TotalRate,
		                    CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ql.DesignNum,0) WHEN 0 THEN 0
		ELSE 100*(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and EndDate<=getdate()) / (1.0 *ql.DesignNum) END AS DECIMAL(9,2)))))+'%'
		AS SumRate,
                            CONVERT(FLOAT,(select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and Months<qc.Months)) as LastTotalPlanNum, 
                            CONVERT(FLOAT,(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where QuantityListId=qc.QuantityListId and Months<qc.Months)) as LastTotalRealNum
                            from [dbo].JDGL_QuantityList ql
							left join dbo.[JDGL_QuantityCompletion] qc on ql.QuantityListId=qc.QuantityListId
where qc.QuantityCompletionId=(select top 1 QuantityCompletionId from [JDGL_QuantityCompletion] q where q.QuantityListId=qc.QuantityListId order by q.EndDate desc)



GO



CREATE TABLE [dbo].[JDGL_SteelStructureCompletion](
	[SteelStructureCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ArrivalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_SteelStructureCompletion] PRIMARY KEY CLUSTERED 
(
	[SteelStructureCompletionId] 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].[JDGL_SteelStructureCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_SteelStructureCompletion_Base_Project] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[Base_Project] ([ProjectId])
GO

ALTER TABLE [dbo].[JDGL_SteelStructureCompletion] CHECK CONSTRAINT [FK_JDGL_SteelStructureCompletion_Base_Project]
GO

ALTER TABLE [dbo].[JDGL_SteelStructureCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_SteelStructureCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_SteelStructureCompletion] CHECK CONSTRAINT [FK_JDGL_SteelStructureCompletion_Base_Unit]
GO

ALTER TABLE [dbo].[JDGL_SteelStructureCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_SteelStructureCompletion_Sys_User] FOREIGN KEY([CompileMan])
REFERENCES [dbo].[Sys_User] ([UserId])
GO

ALTER TABLE [dbo].[JDGL_SteelStructureCompletion] CHECK CONSTRAINT [FK_JDGL_SteelStructureCompletion_Sys_User]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'SteelStructureCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @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'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'总量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'到货数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'ArrivalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'本周期安装数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_SteelStructureCompletion', @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'JDGL_SteelStructureCompletion', @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'JDGL_SteelStructureCompletion'
GO



CREATE VIEW [dbo].[View_JDGL_SteelStructureCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_SteelStructureCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalThisNum,
                            CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_SteelStructureCompletion where UnitId=ss.UnitId and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_SteelStructureCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.SteelStructureCompletionId=(select top 1 SteelStructureCompletionId from JDGL_SteelStructureCompletion q where q.UnitId=ss.UnitId order by q.EndDate desc)





GO



CREATE TABLE [dbo].[JDGL_EquipmentCompletion](
	[EquipmentCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ArrivalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_EquipmentCompletion] PRIMARY KEY CLUSTERED 
(
	[EquipmentCompletionId] 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].[JDGL_EquipmentCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_EquipmentCompletion_Base_Project] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[Base_Project] ([ProjectId])
GO

ALTER TABLE [dbo].[JDGL_EquipmentCompletion] CHECK CONSTRAINT [FK_JDGL_EquipmentCompletion_Base_Project]
GO

ALTER TABLE [dbo].[JDGL_EquipmentCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_EquipmentCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_EquipmentCompletion] CHECK CONSTRAINT [FK_JDGL_EquipmentCompletion_Base_Unit]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'EquipmentCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @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'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'设备总数' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'已到场数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'ArrivalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'安装就位数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_EquipmentCompletion', @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'JDGL_EquipmentCompletion', @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'JDGL_EquipmentCompletion'
GO



CREATE VIEW [dbo].[View_JDGL_EquipmentCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_EquipmentCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalThisNum,
                            CONVERT(FLOAT,ss.ArrivalNum-(select sum(ISNULL(ThisNum,0)) from JDGL_EquipmentCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalLastThisNum,
							CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_EquipmentCompletion where UnitId=ss.UnitId and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_EquipmentCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.EquipmentCompletionId=(select top 1 EquipmentCompletionId from JDGL_EquipmentCompletion q where q.UnitId=ss.UnitId order by q.EndDate desc)





GO



CREATE TABLE [dbo].[JDGL_PipelineCompletion](
	[PipelineCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_PipelineCompletion] PRIMARY KEY CLUSTERED 
(
	[PipelineCompletionId] 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].[JDGL_PipelineCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_PipelineCompletion_Base_Project] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[Base_Project] ([ProjectId])
GO

ALTER TABLE [dbo].[JDGL_PipelineCompletion] CHECK CONSTRAINT [FK_JDGL_PipelineCompletion_Base_Project]
GO

ALTER TABLE [dbo].[JDGL_PipelineCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_PipelineCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_PipelineCompletion] CHECK CONSTRAINT [FK_JDGL_PipelineCompletion_Base_Unit]
GO

ALTER TABLE [dbo].[JDGL_PipelineCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_PipelineCompletion_Sys_User] FOREIGN KEY([CompileMan])
REFERENCES [dbo].[Sys_User] ([UserId])
GO

ALTER TABLE [dbo].[JDGL_PipelineCompletion] CHECK CONSTRAINT [FK_JDGL_PipelineCompletion_Sys_User]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'PipelineCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @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'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'总试压包数' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'本期完成量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_PipelineCompletion', @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'JDGL_PipelineCompletion', @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'JDGL_PipelineCompletion'
GO


CREATE VIEW [dbo].[View_JDGL_PipelineCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_PipelineCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalThisNum,
                            CONVERT(FLOAT,ss.TotalNum-(select sum(ISNULL(ThisNum,0)) from JDGL_PipelineCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalLastThisNum,
							CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_PipelineCompletion where UnitId=ss.UnitId and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_PipelineCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.PipelineCompletionId=(select top 1 PipelineCompletionId from JDGL_PipelineCompletion q where q.UnitId=ss.UnitId order by q.EndDate desc)





GO


CREATE TABLE [dbo].[JDGL_ElectricalCompletion](
	[ElectricalCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[Name] [nvarchar](50) NULL,
	[Unit] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_ElectricalCompletion] PRIMARY KEY CLUSTERED 
(
	[ElectricalCompletionId] 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].[JDGL_ElectricalCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_ElectricalCompletion_Base_Project] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[Base_Project] ([ProjectId])
GO

ALTER TABLE [dbo].[JDGL_ElectricalCompletion] CHECK CONSTRAINT [FK_JDGL_ElectricalCompletion_Base_Project]
GO

ALTER TABLE [dbo].[JDGL_ElectricalCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_ElectricalCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_ElectricalCompletion] CHECK CONSTRAINT [FK_JDGL_ElectricalCompletion_Base_Unit]
GO

ALTER TABLE [dbo].[JDGL_ElectricalCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_ElectricalCompletion_Sys_User] FOREIGN KEY([CompileMan])
REFERENCES [dbo].[Sys_User] ([UserId])
GO

ALTER TABLE [dbo].[JDGL_ElectricalCompletion] CHECK CONSTRAINT [FK_JDGL_ElectricalCompletion_Sys_User]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'ElectricalCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @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'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工作项' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'Name'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'Unit'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工程量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'本期完成量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_ElectricalCompletion', @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'JDGL_ElectricalCompletion', @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'JDGL_ElectricalCompletion'
GO


CREATE VIEW [dbo].[View_JDGL_ElectricalCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_ElectricalCompletion where UnitId=ss.UnitId and Name=ss.Name and EndDate<=getdate())) as TotalThisNum,
							CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_ElectricalCompletion where UnitId=ss.UnitId and Name=ss.Name and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_ElectricalCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.ElectricalCompletionId=(select top 1 ElectricalCompletionId from JDGL_ElectricalCompletion q where q.UnitId=ss.UnitId and q.Name=ss.Name order by q.EndDate desc)





GO


CREATE TABLE [dbo].[JDGL_MeterCompletion](
	[MeterCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[Name] [nvarchar](50) NULL,
	[Unit] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_MeterCompletion] PRIMARY KEY CLUSTERED 
(
	[MeterCompletionId] 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].[JDGL_MeterCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_MeterCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_MeterCompletion] CHECK CONSTRAINT [FK_JDGL_MeterCompletion_Base_Unit]
GO

ALTER TABLE [dbo].[JDGL_MeterCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_MeterCompletion_Sys_User] FOREIGN KEY([CompileMan])
REFERENCES [dbo].[Sys_User] ([UserId])
GO

ALTER TABLE [dbo].[JDGL_MeterCompletion] CHECK CONSTRAINT [FK_JDGL_MeterCompletion_Sys_User]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'MeterCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @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'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工作项' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'Name'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'Unit'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'工程量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'本期完成量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_MeterCompletion', @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'JDGL_MeterCompletion', @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'JDGL_MeterCompletion'
GO


CREATE VIEW [dbo].[View_JDGL_MeterCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_MeterCompletion where UnitId=ss.UnitId and Name=ss.Name and EndDate<=getdate())) as TotalThisNum,
							CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_MeterCompletion where UnitId=ss.UnitId and Name=ss.Name and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_MeterCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.MeterCompletionId=(select top 1 MeterCompletionId from JDGL_MeterCompletion q where q.UnitId=ss.UnitId and q.Name=ss.Name order by q.EndDate desc)





GO



CREATE TABLE [dbo].[JDGL_UndergroundPipeCompletion](
	[UndergroundPipeCompletionId] [nvarchar](50) NOT NULL,
	[ProjectId] [nvarchar](50) NULL,
	[UnitId] [nvarchar](50) NULL,
	[TotalNum] [decimal](18, 2) NULL,
	[ThisNum] [decimal](18, 2) NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[CompileMan] [nvarchar](50) NULL,
	[CompileDate] [datetime] NULL,
 CONSTRAINT [PK_JDGL_UndergroundPipeCompletion] PRIMARY KEY CLUSTERED 
(
	[UndergroundPipeCompletionId] 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].[JDGL_UndergroundPipeCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Base_Project] FOREIGN KEY([ProjectId])
REFERENCES [dbo].[Base_Project] ([ProjectId])
GO

ALTER TABLE [dbo].[JDGL_UndergroundPipeCompletion] CHECK CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Base_Project]
GO

ALTER TABLE [dbo].[JDGL_UndergroundPipeCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Base_Unit] FOREIGN KEY([UnitId])
REFERENCES [dbo].[Base_Unit] ([UnitId])
GO

ALTER TABLE [dbo].[JDGL_UndergroundPipeCompletion] CHECK CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Base_Unit]
GO

ALTER TABLE [dbo].[JDGL_UndergroundPipeCompletion]  WITH CHECK ADD  CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Sys_User] FOREIGN KEY([CompileMan])
REFERENCES [dbo].[Sys_User] ([UserId])
GO

ALTER TABLE [dbo].[JDGL_UndergroundPipeCompletion] CHECK CONSTRAINT [FK_JDGL_UndergroundPipeCompletion_Sys_User]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'UndergroundPipeCompletionId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'项目Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @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'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'UnitId'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'DIN/米' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'TotalNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'本期完成量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'ThisNum'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'开始日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'StartDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'结束日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @level2type=N'COLUMN',@level2name=N'EndDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'编制人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'JDGL_UndergroundPipeCompletion', @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'JDGL_UndergroundPipeCompletion', @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'JDGL_UndergroundPipeCompletion'
GO


CREATE VIEW [dbo].[View_JDGL_UndergroundPipeCompletion]
AS
select distinct ss.*,u.UnitName,
                            CONVERT(FLOAT,(select sum(ISNULL(ThisNum,0)) from JDGL_UndergroundPipeCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalThisNum,
                            CONVERT(FLOAT,ss.TotalNum-(select sum(ISNULL(ThisNum,0)) from JDGL_UndergroundPipeCompletion where UnitId=ss.UnitId and EndDate<=getdate())) as TotalLastThisNum,
							CONVERT(NVARCHAR(10),((CAST(CASE ISNULL(ss.TotalNum,0) WHEN 0 THEN 0
		ELSE 100*ISNULL((select sum(ISNULL(ThisNum,0)) from JDGL_UndergroundPipeCompletion where UnitId=ss.UnitId and EndDate<=getdate()),0) / (1.0 *ss.TotalNum) END AS DECIMAL(9,2)))))+'%'
		AS Rate
                            from dbo.JDGL_UndergroundPipeCompletion ss
							left join dbo.Base_Unit u on ss.UnitId=u.UnitId
where ss.UndergroundPipeCompletionId=(select top 1 UndergroundPipeCompletionId from JDGL_UndergroundPipeCompletion q where q.UnitId=ss.UnitId order by q.EndDate desc)





GO



/*******安全隐患********/
ALTER VIEW [dbo].[View_Technique_RectifyItem]
AS
SELECT M.RectifyItemId, 
	M.RectifyId, 
	M.HazardSourcePoint, 
	M.RiskAnalysis, 
	M.RiskPrevention, 
	M.SimilarRisk,
	R.RectifyName,
	R.RectifyCode,
	M.CompileMan,
	M.CompileDate,
	M.AuditMan,
	M.AuditDate,
	M.IsPass,
	U.UserName AS CompileManName,
	UR.UserName AS AuditManName,
	CAST(0 AS BIT) as IsSelected
FROM dbo.Technique_RectifyItem AS M
LEFT JOIN dbo.Technique_Rectify AS R ON R.RectifyId=M.RectifyId
LEFT JOIN dbo.Sys_User AS U ON U.UserId=M.CompileMan
LEFT JOIN dbo.Sys_User AS UR ON U.UserId=M.AuditMan

GO

INSERT INTO dbo.Sys_Menu(MenuId,MenuName,Url,SortIndex,SuperMenu,MenuType,IsOffice,IsEnd,IsUsed)
VALUES('29F27641-06ED-435A-9F9B-FCE6366801BE','企业监督检查整改','HSSE/Hazard/ProjectSuperviseCheckRectify.aspx',65,'467A0CB9-737D-4451-965E-869EBC3A4BD6','Menu_HSSE',0,1,1)
GO
--企业监督检查整改
INSERT INTO dbo.Sys_ButtonToMenu(ButtonToMenuId,MenuId,ButtonName,SortIndex)
VALUES('B6BC2531-A381-4843-9E08-C49EFB3B541B','29F27641-06ED-435A-9F9B-FCE6366801BE','修改',2)	
INSERT INTO dbo.Sys_ButtonToMenu(ButtonToMenuId,MenuId,ButtonName,SortIndex)
VALUES('FBBC83E5-DF22-4995-8086-CB7A970CE2D3','29F27641-06ED-435A-9F9B-FCE6366801BE','保存',4)  
GO