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
 New to SQL Server Programming
 order by for the second select statement only

Author  Topic 

rajendrarama
Starting Member

14 Posts

Posted - 2011-10-01 : 12:00:29
Hi,

I have a table with column values as 'please select', 'b', 'a', 'c'.
I want the query to perform the result set in the order
'please select'
'a'
'b'
'c'
I tried the query:
select columnname from tablename where columnname ='please select'
union all
select columnname from tablename where columnname <>'please select' order by columnname

It has not worked since order by applies for the whole and results in
'a'
'b'
'c'
'please select'
which is not what i wanted. Can anyone help with the query.

Thanks,

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-01 : 12:12:06
[code]
select columnname from tablename
order by case when columnname ='please select' then 1 else 2 end asc, columnname asc
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-10-02 : 06:37:30
hi ,

you also can try the following code :

select * from (select Top 1 columnname from tablename where columnname ='please select') as xx
union all
select * from (select Top 1 columnname from tablename where columnname <>'please select' order by columnname) as xx

good luck

paul Tech
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-02 : 07:40:05
quote:
Originally posted by paultech

hi ,

you also can try the following code :

select * from (select Top 1 columnname from tablename where columnname ='please select') as xx
union all
select * from (select Top 1 columnname from tablename where columnname <>'please select' order by columnname) as xx

good luck

paul Tech


why splitting it with union all if you can do it in same statement itself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -