🌻

SQLServerでグループ毎に連番を振る

2024/01/04に公開

key1+key2毎に連番、グループ中のどれかを特定するのはkey3とする

UPDATE  table
SET
    [updateColumn] = wt1.rownum
FROM
(
SELECT key1,key2, key3, 
ROW_NUMBER() OVER
(PARTITION BY key1, key2 ORDER BY [sortColumn] DESC) AS rownum
FROM table
) AS wt1
WHERE
    table.key1 = wt1.key1
    table.key2 = wt1.key2
    table.key3 = wt1.key3

Discussion