alter table [dbo].[JDGL_QuantityList] add SortIndex int null
GO


ALTER VIEW [dbo].[View_JDGL_QuantityCompletion]
AS
select qc.*,ql.DesignNum,ql.Name,ql.Unit,ql.SortIndex,
                            CONVERT(FLOAT,(select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where ProjectId=ql.ProjectId and QuantityListId=qc.QuantityListId and EndDate<=getdate())) as TotalPlanNum,
                            CONVERT(FLOAT,(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where ProjectId=ql.ProjectId and 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 ProjectId=ql.ProjectId and QuantityListId=qc.QuantityListId and EndDate<=getdate()) WHEN 0 THEN 0
		ELSE 100*(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where ProjectId=ql.ProjectId and QuantityListId=qc.QuantityListId and EndDate<=getdate()) / (1.0 *(select sum(ISNULL(PlanNum,0)) from JDGL_QuantityCompletion where ProjectId=ql.ProjectId and 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 ProjectId=ql.ProjectId and 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 ProjectId=ql.ProjectId and QuantityListId=qc.QuantityListId and Months<qc.Months)) as LastTotalPlanNum, 
                            CONVERT(FLOAT,(select sum(ISNULL(RealNum,0)) from JDGL_QuantityCompletion where ProjectId=ql.ProjectId and 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


