Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there,

Is there an opposite to TOP , something like a Least Value or latest value.


Thank You
Best Regards.
Posted

Just use TOP, but change the sort order to DESC from ASC.

TOP only makes sense when you use an ORDER BY clause anyway, as the DB is not required to return records in any particular order otherwise.
 
Share this answer
 
Comments
Member 8956437 24-May-12 7:53am    
Yes your right, but im using Union aswell and order by is not working
OriginalGriff 24-May-12 8:37am    
Then you need to fix the SQL first! :laugh:
Have a look here for an example of UNION with ORDER BY:
http://www.techonthenet.com/sql/union.php
Member 8956437 24-May-12 9:05am    
That example is not exactly what i was looking for, i need to use the Order By in each of my Selects
OriginalGriff 24-May-12 10:53am    
Sorry?
You can't use ORDER BY pre-UNION, because the UNION will impose it's own (not necessarily sensible) order on things.
The order of items on either side of a UNION is irrelevant, since it just removes duplicates or combines data into a single list.
Member 8956437 24-May-12 11:14am    
ok, but im using Top 5 and i needed to also use de Order By DESC, so i can select the latest data.
Bases on the conversation if I understood your question correctly, you can possibly use inline views. However, before jumping to the SQL, I would suggest going through your sql statement and/or table structure since it sounds like they should be corrected.

Anyhow, consider the following
SQL
select top 5 name
from (select name 
      from (select top 100 percent name 
            from sysobjects 
            order by name) as a
      union all
      select name 
      from (select top 100 percent name 
            from syscolumns 
            order by id) as b) as c
order by name desc
 
Share this answer
 
Comments
Maciej Los 24-May-12 17:08pm    
Good answer, my 5!
Wendelius 25-May-12 0:26am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900