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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Order by in a Subquery

Author  Topic 

k_cire0426
Yak Posting Veteran

63 Posts

Posted - 2009-11-12 : 01:35:43
Hello,

Example data.
col col1 col2
1 a type1
0 u type2
2 t type1
5 k type2
3 y type1
4 b type1

I want to return the "2 t type1" in my query.

Select top 1 * from (Select top 2 * from table where col2 = type1) AliasTable Order by col desc

When I ran my query the result is "4 b type1"..

Any idea?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-12 : 02:05:41
Select top 1 * from (Select top 2 * from table where col2 = 'type1' order by col) AliasTable Order by col desc

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

k_cire0426
Yak Posting Veteran

63 Posts

Posted - 2009-11-12 : 02:52:49
wow... ^_^ it worked..may i know what is behind on your query?..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-12 : 04:06:19
quote:
Originally posted by k_cire0426

wow... ^_^ it worked..may i know what is behind on your query?..


I included order by inside the derived table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -