Search code examples
sqlsql-servert-sqldatewindow-functions

Get previous cell value + side cell value calculation in SQL Server


Image

I wan this type of calculated date value in SQL.

Is any way to get this type of calculated data ?


Solution

  • I think that you want:

    select
        t.*,
        dateadd(
            day, 
            sum(t.duration) over(order by autoid), 
            first_value(t.date) over(order by autoid)
        ) date
    from mytable t
    

    Starting from the first value in the date column (which, as I understand, is the only non-null value in that column), this incrementally adds the number of days in the duration column.