Row numbers with partitions
suggest changePARTITION BY starts the numbering again for each group. ORDER BY DepartmentId does not order rows inside a department, because every row in the partition has the same DepartmentId. Add a unique column to the ORDER BY if the numbers within a department must be stable.
SELECT
ROW_NUMBER() OVER(PARTITION BY DepartmentId ORDER BY DepartmentId ASC) AS RowNumber,
DepartmentId, Fname, LName
FROM Employees
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents