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 identifier "tableName" could not be bound.
Not sure how to fix the error.
Thanks!
Output should look like the following:
columnOfNumbers Difference
1 NULL
2 1
3 1
10 7
12 2
.... ....
Code:
USE DATABASE;
WITH CTE AS
(SELECT
ROW_NUMBER() OVER (PARTITION BY tableName ORDER BY columnOfNumbers) ROW,
columnOfNumbers
FROM tableName)
SELECT
a.columnOfNumbers
FROM
CTE a
LEFT JOIN CTE b
ON a.columnOfNumbers = b.columnOfNumbers AND a.ROW = b.ROW + 1
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…