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
 sorting query

Author  Topic 

gagani
Posting Yak Master

112 Posts

Posted - 2012-05-31 : 10:37:56
select orderid, customer_name from customer
where orderid in
(
530076,
560111,
524091,
804141
) order by customer_name

The output is

530076 HORN
560111 JOHN
524091 KEITH
804141 ADAM RUSSELL

Is there anyway I can get the output as
530076 ADAM RUSSELL
560111 HORN
524091 JOHN
804141 KEITH

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-05-31 : 10:42:23
Shoould be that. Are you sure you have the order by statement? Where do you output the data to?
If you are inserrting into a table the optimiser can ignore the order by clause as table data is not ordered.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 2012-05-31 : 10:51:28
yes, i am using the order by

The output is

530076 HORN
560111 JOHN
524091 KEITH
804141 ADAM RUSSELL

but i want the output as
804141 ADAM RUSSELL
530076 HORN
560111 JOHN
524091 KEITH
Go to Top of Page

gagani
Posting Yak Master

112 Posts

Posted - 2012-05-31 : 10:55:15
It is doing alphabetical ordering from the table which has got only one word as customer_name and then it is doing alphabetical ordering for the customer_name with two words
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-05-31 : 11:18:48
As far as the server is concerned it only has one name - some have a space in them.
A should come before H - can you post the data.
Give a table variable, populate it and give the query

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-05-31 : 11:21:15
Something like

declare @t table (i int, s varchar(20))
insert @t select 1,'hhh'
insert @t select 2,'jjj'
insert @t select 3,'kkk'
insert @t select 4,'aa zzzzzz'
insert @t select 5,'hhh'
insert @t select 6,'hhh'

select *
from @t
where i in (1,2,3,4)
order by s

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -