SQL - Running Total - Year To Date, Previous Year To Date, and Last Rolling 12 Months

旧街凉风 提交于 2021-02-05 12:30:49

问题


Thanks for helping me out with this one! I apologize in advance for the formatting, but I think it is easy to follow.

Provided is a sample table of data. I need three separate columns created with query language. The running total must go consider the ID and the date.

Running 12 Months - I need sum the value of each ID for rolling 12 months.

Current YTD - Sum the value of each ID for the current year of the date of the value.

Previous YTD - Sum the value of each ID for the year immediately prior to the date of the value.

THANK YOU!

CURRENT TABLE

ID  DATE        VALUE
S1  01/01/2015  5
S1  02/01/2015  5
S1  03/01/2015  5
S1  04/01/2015  5
S1  05/01/2015  5
S1  06/01/2015  5
S1  07/01/2015  5
S1  08/01/2015  5
S1  09/01/2015  5
S1  10/01/2015  5
S1  11/01/2015  5
S1  12/01/2015  5
S1  01/01/2016  5
S2  01/01/2015  10
S2  02/01/2015  10
S2  03/01/2015  10
S2  04/01/2015  10
S2  05/01/2015  10
S2  06/01/2015  10
S2  07/01/2015  10
S2  08/01/2015  10
S2  09/01/2015  10
S2  10/01/2015  10
S2  11/01/2015  10
S2  12/01/2015  10
S2  01/01/2016  10

DESIRED OUTPUT

ID  DATE            VALUE   Running12   CalendarYTD PrevCalendarYTD
S1  01/01/2015      5       5           5   
S1  02/01/2015      5       10          10  
S1  03/01/2015      5       15          15  
S1  04/01/2015      5       20          20  
S1  05/01/2015      5       25          25  
S1  06/01/2015      5       30          30  
S1  07/01/2015      5       35          35  
S1  08/01/2015      5       40          40  
S1  09/01/2015      5       45          45  
S1  10/01/2015      5       50          50  
S1  11/01/2015      5       55          55  
S1  12/01/2015      5       60          60  
S1  01/01/2016      5       60          5              5
S2  01/01/2015      10      10          10  
S2  02/01/2015      10      20          20  
S2  03/01/2015      10      30          30  
S2  04/01/2015      10      40          40  
S2  05/01/2015      10      50          50  
S2  06/01/2015      10      60          60  
S2  07/01/2015      10      70          70  
S2  08/01/2015      10      80          80  
S2  09/01/2015      10      90          90  
S2  10/01/2015      10      100         100
S2  11/01/2015      10      110         110 
S2  12/01/2015      10      120         10  
S2  01/01/2016      10      120         10              10

回答1:


VKP Raised a valid point. If missing months, we would need to fill.

The following will generate your running total columns.

Example

Select *
      ,Running12       = sum(Value) over (Partition By ID Order By Date Rows Between 11 Preceding and Current Row)
      ,CalendarYTD     = sum(Value) over (Partition By ID,Year(Date) Order By Date)
      ,PrevCalendarYTD = case when month(date)<>1 then null else (Select Value from @YourTable Where ID=A.ID and date=dateadd(year,-1,A.date)) end
 From @YourTable A
 Order By ID,Date

Returns



来源:https://stackoverflow.com/questions/43942914/sql-running-total-year-to-date-previous-year-to-date-and-last-rolling-12-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!