Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
melchor010
Starting Member
2 Posts |
Posted - 2011-02-18 : 18:19:11
|
I have a table with the following fields:StudentNo, Class, Grade. I added a new column called sequence. I have close to 5000 records to update. How do I populate the column so that the table will look like below?Student Class Sequence Grade1 A 1 801 B 2 751 C 3 822 D 1 722 E 2 86Thank you so much.Melchor |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-02-19 : 00:17:39
|
You have not mentioned what version of SQL you are using. If you are using 2005 or higher then the below logic should work for you.--Sample table structure.Create table #Students(StudentNo int,Class varchar(50),Grade int)Insert into #StudentsSelect 1,'A', 80 union allSelect 1 ,'B', 75 union allSelect 1 ,'C',82 union allSelect 2 ,'D',72 union allSelect 2 ,'E', 86Alter table #StudentsAdd Seq int--Actual Logic for updation starts here;with cteas(Select Row_number() Over (Partition by StudentNo order by StudentNo) as Srno,* from #Students)update cte set seq = srnoselect * from #Students |
|
|
|
|
|
|
|