Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help for my sql statement

Status
Not open for further replies.

Mayo123

Technical User
Apr 23, 2005
58
US
Good morning, everyone!
I have a sql statement
SELECT TOP 20 WITH TIES ssn, LName, FInitial, SUM(hrsWorked) AS [Total HrsWorked], SUM(amtEarned) AS [Total AmtEarned]
FROM MyDetailedTable
GROUP BY ssn, LName, FInitial
Order by SUM(hrsWorked) desc, SUM(amtEarned) desc

I need to have a field called Rank to be inserted into this select statement which will have the number running from 1 to 20 each time the statement is executed.

How can I implement this?

Thanks in advance


 
SQL2000 or 2005?

Also: what kind of rank? Vanilla, dense or simple row enumeration (1, 2, 3...)?

And WITH TIES may return more than 20 rows...

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
Sorry about confusing.
I'm using sql 2000
The reason I need a rank is on my form to show who's ranked No 1,2,3 etc...It should be a simple row enumeration (1, 2, 3...)when running the statement.
Thanks!
 
Arguably the simplest way is with identity table:
Code:
select identity(tinyint, 1, 1) as rank, X.*
into #tmp
from
(	<your query with TOP and ORDER BY here>
) X

select * from #tmp
drop table #tmp

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top