Translate

Monday, August 23, 2021

TSQL - Assign A Sequential Number To Each Record In A Group

I needed to insert records from one table into another table and add a sequential number, field seq, that was group on an id field, GroupId. This CTE was the eastiest way that I found to add the number.

UPDATE t
SET seq = GROUPSEQ
FROM (SELECT seq, [GroupId], row_number() OVER (PARTITION BY [GroupId] ORDER BY [GroupId]) GROUPSEQ
FROM [TableMemo]) t

No comments:

Post a Comment

Thank you for commenting!