Tuesday, January 22, 2008

Order By Sub-query

In case you never heard of this, you can actually create a select statement in which the Order By clause can be a sub-query too.

SELECT BranchID,

( SELECT TOP 1 InstitutionName FROM Institution WHERE Institution.InstitutionID=FIServiceLocation.InstitutionID) InstitutionName

FROM FIServiceLocation

Order By (SELECT TOP 1 InstitutionName FROM Institution WHERE Institution.InstitutionID=FIServiceLocation.InstitutionID)

The feature actually is very useful for me in a project I am working on, since I wanted to be able to sort a computed field.

No comments: