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
 generate sequence no in one column

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-03-20 : 09:06:09
helen writes "I have a sql table:
NAME AGE
Anna 10
Susan 5
Dina 12

Please help me to come up with a QUERY STATEMENT with this result:
SEQNO NAME AGE
1 Anna 10
2 Susan 5
3 Dina 12 "

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-20 : 09:08:35
[code]
select SEQNO = (ROW_NUMBER() OVER (ORDER BY AGE)),
NAME, AGE
from sqltable
[/code]


KH

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2007-03-20 : 09:10:56
If name is unqiue in your table then.. this is the way to get the SEQNo

SELECT (SELECT COUNT(i.Name)
FROM YourTable i
WHERE i.Name >= o.Name) AS SEQNO,
*
FROM YourTable o
ORDER BY RowID

And if you using SQL SERVER 2005

http://www.sqlservercentral.com/columnists/mcoles/sequentialordering.asp

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page
   

- Advertisement -