0

I have a table with ID and sequence # (SEQ) starting number. I want to update SQ number for each ID based on the SEQ

My table like this

ID       SQ    SEQ
---      --    ---
100      0      11
100      0      11
100      0      11
200      0      13
200      0      13
200      0      13
200      0      13

I want to get result table like this:

ID     | SQ | SEQ

100      11      11
100      12      11
100      13      11
200      13      13
200      14      13
200      15      13
200      16      13

How can I do this using TSQL?

2 Answers 2

1

Try this

select id,(ROW_NUMBER() over(partition by id order by id) + seq - 1) as sqn,seq from @tbl

Complete sample code with test data

declare @tbl table(id int, sq int, seq int)
insert into @tbl
select 100,0,11 union all
select 100,0,11 union all
select 100,0,11 union all
select 200,0,13 union all
select 200,0,13 union all
select 200,0,13 union all
select 200,0,13 

select id,(ROW_NUMBER() over(partition by id order by id) + seq - 1) as sqn,seq from @tbl

Result

id  sqn seq
100 11  11
100 12  11
100 13  11
200 13  13
200 14  13
200 15  13
200 16  13
Sign up to request clarification or add additional context in comments.

Comments

0

try it's.

SELECT 
ID,ROW_NUMBER() OVER(ORDER BY ID ) AS SQ,SEQ
FROM YOUR_TABLE

And for update, use it's

 update a set sq = rn
    ID,ROW_NUMBER() OVER(ORDER BY ID ) AS rn,SEQ
    FROM YOUR_TABLE) as a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.