I am trying to update the SeqNum column value based on the StudNo, StudentName. There will be sequence which should be incremented for every same set of StudNo, StudentName.
create table #student (
StudNo int,
StudentName char(50),
SeqNum int default 1, - newly added column
StudAddress char(50)
)
Before have the seqNum(Before update data):
StudNo StudentName StudAddress
1 Ravi C
2 Ram XYZ
1 Ravi A
1 Ravi B
3 Ram HJK
Below is the output data which we need to get(After update):
StudNo StudentName SeqNum StudAddress
1 Ravi 1 C
2 Ram 1 XYZ
1 Ravi 2 A
1 Ravi 3 B
3 Ram 1 HJK
In the above output data we have StudNo 1 and StudentName 'Ravi' in the 1st row, 3rd row, 4th row. So the SeqNum column value is 1, 2,3 respectively.
Below is the updated query I have which will not update as expected.
update Student set seqNum=seq+1 where StudNo in (select StudNo from Student group by StudNo,StudentName having count(*) > 1)
and StudentName in (select StudentName from Student group by StudNo,StudentName having count(*) > 1)
Note: We can get output data order StudNo and StudAddress
Could anyone give me a thought on how to write update query to increment the SeqNum column values based on the set of other columns.
Thanks in Advance