update incremental values into an SQL column
I'm looking for some help with the following query:
I have a bunch of rows with column TYPE, VERSION - however in some cases the version got messed up so I want to rewrite it.
Basically, it now looks this:
Type, Version
A, 0
A, 0
A, 1
A, 2
B, 1
B, 3
I want it to look like this:
Type, Version
A, 0
A, 1
A, 2
A, 3
B, 开发者_C百科0
B, 1
Any help would be greatly appreciated, Thanks
WITH T AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Type ORDER BY Version) - 1 AS V
FROM YourTable
)
UPDATE T
SET Version = V
精彩评论