I have the following table test
test
iD Name ParentId GroupID ----------------------------- 1 1 Null 2 1 Null 3
; WITH CTE AS ( SELECT iDName, ParentId, GroupID, DENSE_RANK() OVER (ORDER BY ParentId ASC) RN FROM Test ) UPDATE CTE SET GroupID = RN