循环遍历同一查询以构建更改一个变量的历史表

时间:2016-09-11 14:05:23

标签: sql sql-server sql-server-2008

我只读取了对SQL Server 2008的访问权限,我需要根据过去(即最近15个月)的不同时间点构建一个百分比历史表。需要更改的唯一值是***行是GETDATE()之后的-0。该值需要更改-0,-1,-2 ...- 15。我正在插入临时表。任何建议将不胜感激。

declare @enddate datetime
declare @end1195 datetime
declare @beg1195 datetime
declare @enddos datetime
declare @begdos datetime
declare @period nvarchar(7)

***set @enddate = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-0, -1)  
set @end1195 = DATEADD(MONTH, DATEDIFF(MONTH, -1, (@enddate))-1, -1) 
set @beg1195 = DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month, -6, @enddate)), 0)  
set @enddos = DATEADD(MONTH, DATEDIFF(MONTH, -1, (@enddate))-0, -1)  
set @begdos= DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month, -0, @enddate)), 0)
set @period = cast(MONTH(@enddate) as nvarchar(2)) + '-' + cast(YEAR(@enddate) as nvarchar(4)) 

--1195 BY Facilityid excluding ANES
insert into  ##Fac1195
    select 
        A.Facilityid, cast(1 as int) as Facility,  
        A.FSC_DESC, @period as Period, @begdos as DOS_Beg, 
        @enddos as DOS_End,
        ROUND(sum(A.adjustments)/sum(A.charges), 2) as [1195]  
    from
        (select 
             v.Facilityid, v.localfacility, v.VisitID, v.AccountNumber, 
             v.DOS, FSC.PrimayPayer  as PayerName, 
             FSC.PrimaryCorpFSC as FSC_DESC,
             chg.charges, isnull(adj.Adjustments,0) as Adjustments
             ........

0 个答案:

没有答案