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
To calculate a difference, you need a pair of records; those two records are “the current record” and “the previous year's record”. You obtain this record using the LAG() window function. This function allows you to obtain data from the previous record (based on an order criterion, which here is “ ORDER BY year ”).
Here's the SQL query to compare each row with previous row. In the above query, we join sales table with itself using an INNER JOIN condition g2.id=g1.id + 1 that allows you to compare each row with its previous row. Please note, this condition depends on the fact that our id column has consecutive numbers.
SQL Server DIFFERENCE() Function The DIFFERENCE() function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values, from 0 to 4. 0 indicates weak or no similarity between the SOUNDEX values. 4 indicates strong similarity or identically SOUNDEX values.
Find Row Differences in ExcelClick “Find & Select” and pick “Go To Special” in the drop-down list. In the window that pops open, choose “Row Differences” and click “OK.” The window will automatically close, and you'll see the differences in your rows highlighted.
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.
See sqlFiddle
;WITH tblDifference AS
(
SELECT ROW_NUMBER() OVER(ORDER BY id) AS RowNumber, columnOfNumbers
FROM tableName
)
SELECT cur.columnOfNumbers, cur.columnOfNumbers - previous.columnOfNumbers
FROM tblDifference cur
LEFT OUTER JOIN tblDifference previous
ON cur.RowNumber = previous.RowNumber + 1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With