I\'m try to select a certain rows from my table using the row_number over. However, the sql will prompt the error msg \"Invalid column name \'ROWNUMBERS\' \". Anyone can cor
Attempting to reference the aliased column in the WHERE
clause does not work because of the logical query processing taking place. The WHERE
is evaluated before the SELECT
clause. Therefore, the column ROWNUMBERS
does not exist when WHERE
is evaluated.
The correct way to reference the column in this example would be:
SELECT a.*
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Price ASC) AS ROWNUMBERS, *
FROM Product) a
WHERE a.ROWNUMBERS BETWEEN @fromCount AND @toCount
For your reference, the order for operations is:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY