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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 sequence number in query result

Author  Topic 

chadbryant5
Starting Member

32 Posts

Posted - 2005-04-15 : 10:41:41
This is probably a stupid question, but how do I get a sequence # column in my T-SQL query? If I run a query in Query Analyzer, I get a grid that has the resultset. Down the left side, each row is numbered 1...n. I'd just like this numbering to exist as a column.

IE:

Value Frequency Sequence
---------------------------------
10203 3334 1
10399 3300 2
.
.
.
12993 4 13000

Is this possible?

Thanks in advance

Chad

PW
Yak Posting Veteran

95 Posts

Posted - 2005-04-15 : 10:44:28
You'd need to create a temp table or table variable with an Identity column, select your resultset into that table with an ORDER BY, then select the final result out of the temp table/variable.

In SQL2005, you'll be able to do this with the new Row_Number ranking function.
Go to Top of Page

chadbryant5
Starting Member

32 Posts

Posted - 2005-04-15 : 10:51:37
Ok, that makes sense. I'm suprised t-sql at this point doesn't have a way to do this though. Anyway, thanks for the tip!!

Chad
Go to Top of Page
   

- Advertisement -