Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Get data from Previous record (Cumulative Data)-- Urgent help required RE: Get data from Previous record (Cumulative Data)-- Urgent help required

  • I think this is right. I am kinda tight on time so I didn't have time to look super close but this should at least get you close.

    You can find the tally table here. http://www.sqlservercentral.com/articles/62867/[/url]

    with MyDates as

    (

    select dateadd(MONTH, -1 * N, dateadd(mm, datediff(mm, 0, getdate()), 0)) as PrjDate, x.PRJ_ID

    from Tally t

    cross join (select PRJ_ID from Rev group by PRJ_ID) x

    where N >= 1

    and N <= 24

    )

    select d.PrjDate, d.PRJ_ID, x.AMOUNT

    from MyDates d

    outer apply (select top 1 PRJ_ID, Amount from Rev where Rev.PrjDate <= d.PrjDate and Rev.PRJ_ID = d.PRJ_ID order by Rev.PrjDate desc) x

    order by d.PRJ_ID, d.PrjDate

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/