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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sequential row number

Author  Topic 

laptop545
Starting Member

11 Posts

Posted - 2010-01-13 : 16:27:33
HI all..can any please answer me this question...

We have a table with ids, first names, last name so on..

Then how to select the data, and for each row provide a corresponding sequential row number.



e.g. if the original table had:

id

123

456

789

146





The result should look like:

# id

1 123

2 456

3 789

4 146



This is similar to creating an identity column, but without modifying the existing table. Can any1 please help me...

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-13 : 16:28:54
If you are using SQL server 2005 or higher...
select row_number() over(order by id) as seq, * from table1


EDIT: This will create the sequence number in the order of "id"..in your exampleam not sure how you get 146 in the end? Is there a specific order?
Go to Top of Page

laptop545
Starting Member

11 Posts

Posted - 2010-01-13 : 16:38:20
Tnx for the info...actually its not 146...its 946
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-13 : 17:32:41
Yeah Ok. That makes sense.
Go to Top of Page
   

- Advertisement -