Getting sum of current and next row in sql server 2008/2005

  • Dear Sirs

    My data is as below till 3rd columns

    YearMonthSumsalesCurr_Next
    201612100220
    20171120170
    2017250190
    20173140310
    20174170170

    Output is needed as per above having first three coloums as above plus last column (having addition of current and next row). Please suggest.

  • Without DDL I can't write the query...

    Calculate a row_number over the partition and order that you want for the sum. Do that in a CTE. Then join that to itself on the partition columns and row_number of one = (row number of the other) -1. Then you'll have the two sales as columns and you can trivially add them together.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply