SQL Row_Number() function in Where Clause

To get around this issue, wrap your select statement in a CTE, and then you can query against the CTE and use the windowed function’s results in the where clause.

WITH MyCte AS 
(
    select   employee_id,
             RowNum = row_number() OVER ( order by employee_id )
    from     V_EMPLOYEE 
    ORDER BY Employee_ID
)
SELECT  employee_id
FROM    MyCte
WHERE   RowNum > 0

Leave a Comment