0
我爲我的表創建了一個默認值爲0的列。我想用正確的序列號更新那張表。我可以用Row_Number來做到嗎?基於row_number的更新列
我的表是:
PersonID | Code | Sequence Number
---------+--------+------------
10 | D112 | 0
10 | D112 | 0
10 | D112 | 0
10 | E110 | 0
10 | E110 | 0
10 | E110 | 0
10 | D112 | 0
10 | D112 | 0
10 | D112 | 0
10 | E110 | 0
10 | E110 | 0
10 | E110 | 0
我希望我的表是這樣的:
PersonID | Code | Sequence Number
---------+--------+------------
10 | D112 | 1
10 | D112 | 1
10 | D112 | 1
10 | E110 | 2
10 | E110 | 2
10 | E110 | 2
11 | M490 | 1
11 | M490 | 1
11 | M490 | 1
11 | N550 | 2
11 | N550 | 2
11 | N550 | 2
這是代碼,我有,但不知道這是正確的。
WITH CTE AS (
SELECT
t.Sequence Number,
ROW_NUMBER() OVER (PARTITION BY t.PersonID, t.Code ORDER BY t.PersonID) AS RN
FROM Table AS t)
UPDATE CTE
SET Sequence Number = RN
查看select語句是否正確 –