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
 General SQL Server Forums
 Database Design and Application Architecture
 Order By Union

Author  Topic 

BendJoe
Posting Yak Master

128 Posts

Posted - 2012-03-30 : 12:10:28
Look at this query,
Select '0' as Id, 'Select Name' as Name union Select Id,Name from table order by Name.

This one is ordering the entire result. What I need is to order the second part of the union alone,so that I can display the result in a dropdown list with 'Select Name' always on top and then the rest of the result sorted. How can I do this.
Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-30 : 12:18:56
SELECT Id, Name FROM (
Select 1 as RowID, '0' as Id, 'Select Name' as Name union Select 2 AS RowID, Id,Name from table) AS XXX
order by RowID, Name

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2012-04-03 : 20:52:51
You should really add the "select name" to your list in the UI, not try to force it into the query. I know some of the frameworks and controls out there won't easily let you but if you can, do.
Go to Top of Page
   

- Advertisement -