SQL Find difference between previous and current row

后端 未结 3 1850
太阳男子
太阳男子 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条回答
  •  误落风尘
    2020-12-06 05:58

    I don't think you need the partition by statement:

    WITH CTE AS (
          SELECT ROW_NUMBER() OVER (ORDER BY columnOfNumbers) as ROW,
                 columnOfNumbers
          FROM tableName
         )
    SELECT a.columnOfNumbers, a.columnOfNumbers - b.columnOfNumbers
    FROM CTE a LEFT JOIN
         CTE b
         ON a.ROW = b.ROW + 1;
    

    If you do need it, you should put in a column name as opposed to a table name.

提交回复
热议问题