SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to populate a newly added column?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

melchor010
Starting Member

2 Posts

Posted - 02/18/2011 :  18:19:11  Show Profile  Reply with Quote
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 Grade
1 A 1 80
1 B 2 75
1 C 3 82
2 D 1 72
2 E 2 86

Thank you so much.

Melchor

pk_bohra
Flowing Fount of Yak Knowledge

India
1182 Posts

Posted - 02/19/2011 :  00:17:39  Show Profile  Reply with Quote
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 #Students
Select 1,'A', 80 union all
Select 1 ,'B', 75 union all
Select 1 ,'C',82 union all
Select 2 ,'D',72 union all
Select 2 ,'E', 86


Alter table #Students
Add Seq int

--Actual Logic for updation starts here

;with cte
as
(
Select Row_number() Over (Partition by StudentNo order by StudentNo) as Srno,
* from #Students
)

update cte set seq = srno

select * from #Students


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000