tags:

views:

51

answers:

1
+2  Q: 

SQL Syntax Issues

I can't for the life of me figure out what's wrong with this sql statement

select top(1) 
  OrderNumber 
 from (select top(5) 
         OrderNumber 
       from SomeTable
       where TreePath='tests' 
       order by OrderNumber asc) 

Any Ideas?

Regards, Byron Cobb.

+6  A: 

You need to alias the derived table

select top 1 
  myDerivedTable.OrderNumber 
 from (select top 5 
         OrderNumber 
       from TABLE 
       where TreePath='tests' 
       order by OrderNumber asc) myDerivedTable
order by myDerivedTable.OrderNumber
John Hartsock
Both ways are valid for TOP
Alan Geleynse
@Alan ... thanks ...Learn something new everyday.
John Hartsock
Perfect - Thanks! SQL always gets the better of me :(
Byron Cobb
As it stands this answer is not deterministic. It needs an `order by` on the outer query as well.
Martin Smith