SQL Row_Number() function in Where Clause

后端 未结 10 1526
野性不改
野性不改 2020-12-02 16:11

I found one question answered with the Row_Number() function in the where clause. When I tried one query, I was getting the following error:

10条回答
  •  醉话见心
    2020-12-02 17:01

    Using CTE (SQL Server 2005+):

    WITH employee_rows AS (
      SELECT t.employee_id,
             ROW_NUMBER() OVER ( ORDER BY t.employee_id ) 'rownum'
        FROM V_EMPLOYEE t)
    SELECT er.employee_id
      FROM employee_rows er
     WHERE er.rownum > 1
    

    Using Inline view/Non-CTE Equivalent Alternative:

    SELECT er.employee_id
      FROM (SELECT t.employee_id,
                   ROW_NUMBER() OVER ( ORDER BY t.employee_id ) 'rownum'
              FROM V_EMPLOYEE t) er
     WHERE er.rownum > 1
    

提交回复
热议问题