September 5th, 2003, 09:56 PM
I'm having a problem with a SQL statement in a stored procedure, I am building the statement in the procedure because I want to vary the 'order by' clause. In effect I want the top ranking 5 employees to stay the same regardless of the order by. At the moment the statement is ordering the returned data and taking the top 5 out of that list rather than getting the top 5 and then ordering those results.
select top 5 employee, post, rank, date, salary from sales where rank <> 0 and year=2002 order by salary
any ideas welcome.
September 9th, 2003, 12:09 AM
not sure which field you want the top 5 of, whether asc or desc, and which final order you want...
... but you should be able to figure it out from this:
select top 5
where rank <> 0
by rank desc
) as derivedtable
by salary desc