UPDATE t
SET seq = GROUPSEQ
FROM (SELECT seq, [GroupId], row_number() OVER (PARTITION BY [GroupId] ORDER BY [GroupId]) GROUPSEQ
FROM [TableMemo]) t
No words wasted! Getting to the point about the work I do, the problems I deal with, and some links to posts about where I work.
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.
Wednesday, August 18, 2021
TSQL - Add a Sequential Number Field
I needed to insert records from one table into another table and add a sequential number that started at a certain value. This was the eastiest way that I found to add the number. Just add an int field to the source table and add the values using an update. So in the following code, id was the int field that I created, and I add numbers begining with 6001.
DECLARE @id INT
SET @id = 6000
UPDATE Table
SET @id = id = @id + 1
Subscribe to:
Posts (Atom)