开发者

update incremental values into an SQL column

开发者 https://www.devze.com 2023-04-11 18:49 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号