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
 Row Number

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2008-01-09 : 11:27:04
Is there a way to take the row number from my code and put that into a column I have code that returns the top 50 of sales and I want to put those row numbers into columns that they coordinate with.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-01-09 : 11:32:25
if you are using SQL Server 2005 use row_number()


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

gavakie
Posting Yak Master

221 Posts

Posted - 2008-01-09 : 11:37:04
Sorry Im running 2000
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-09 : 11:38:51
One way is to put the data in sorted order into a temp table with an IDENTITY column and use this value as row number.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-01-09 : 11:39:19

You still can do something similar like

select row_no = (select count(*) from table x where x.pk <= t.pk)
from table t


But it is best to do this in your front end application.



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

gavakie
Posting Yak Master

221 Posts

Posted - 2008-01-09 : 11:46:58
thanks for your help Im new enough to this to already be confused
Go to Top of Page

gavakie
Posting Yak Master

221 Posts

Posted - 2008-01-09 : 12:21:40
So Im trying this below and for my rownumber it keeps returning 20 as the rownum, Im trying to get it to return what number there on so not 20 but 1,2,3,4,5,6,7,8,9 etc.

Select (Select count(*) from #ttTemp2 Where SalesA >= SalesB) as RowNum, *
From #ttTemp2
Go to Top of Page
   

- Advertisement -