SQL Find difference between previous and current row

后端 未结 3 1839
太阳男子
太阳男子 2020-12-06 05:12

I am trying to find the difference between the current row and the previous row. However, I am getting the following error message:

The multi-part id

3条回答
  •  -上瘾入骨i
    2020-12-06 06:02

    If you in SQL Server 2012+ You can use LAG.

     SELECT columnOfNumbers
           ,columnOfNumbers - LAG(columnOfNumbers, 1) OVER (ORDER BY columnOfNumbers)
       FROM tableName
    

    Note: The optional third parameter of LAG is:

    default

    The value to return when scalar_expression at offset is NULL. If a default value is not specified, NULL is returned. default can be a column, subquery, or other expression, but it cannot be an analytic function. default must be type-compatible with scalar_expression.

提交回复
热议问题