Helpful Information
 
 
Category: MS SQL Development
Ordering Problem

Hi,

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.

e.g.
select top 5 employee, post, rank, date, salary from sales where rank <> 0 and year=2002 order by salary

any ideas welcome.

Robin Bailey

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 employee
, post
, rank
, date
, salary
from (
select top 5
employee
, post
, rank
, date
, salary
from sales
where rank <> 0
and year=2002
order
by rank desc
) as derivedtable
order
by salary descrudy
http://r937.com/










privacy (GDPR)