Row numbers with partitions

suggest change

PARTITION 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents