How to Determine Values for Missing Months based on Data of Previous Months in T-SQL

前端 未结 7 1691
庸人自扰
庸人自扰 2020-12-09 14:24

I have a set of transactions occurring at specific points in time:

CREATE TABLE Transactions (
    TransactionDate Date NOT NULL,
    TransactionValue Intege         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-09 14:54

    I don't have access to BOL from my phone so this is a rough guide...

    First, you need to generate the missing rows for the months you have no data. You can either use a OUTER join to a fixed table or temp table with the timespan you want or from a programmatically created dataset (stored proc or suchlike)

    Second, you should look at the new SQL 2008 'analytic' functions, like MAX(value) OVER ( partition clause ) to get the previous value.

    (I KNOW Oracle can do this 'cause I needed it to calculate compounded interest calcs between transaction dates - same problem really)

    Hope this points you in the right direction...

    (Avoid throwing it into a temp table and cursoring over it. Too crude!!!)

提交回复
热议问题