Reputation: 1579
I have a set of data where the combination of programid and rev_id makes primary key. My program Id is redundant for a different reason. So I want to add identity number to rev_id so the primary key rule is not violated.
My current data:
Expected result:
Any help?!
Rextester link: https://rextester.com/SJV93546
Upvotes: 0
Views: 38
Reputation: 1271231
I would suggest expressing this as:
select programID,
(rev_id * 10 + row_number() over (partition by rev_id order by content asc)
) as rev_id,
content
from currentdata;
I see no reason to convert this to a string.
Upvotes: 0
Reputation: 4061
You can use row_number function with partition
select
programID,
cast(rev_id as varchar(6)) + cast(ROW_NUMBER() OVER(PARTITION BY rev_id ORDER BY content ASC) as varchar(2)),
content
from currentdata
Upvotes: 2