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

Author  Topic 

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2010-01-04 : 09:51:41
Dear friends
Hope you are all well!

I am trying to resolve an order by issue and would appreciate some assistance with.

I have a column(called random) that is full of random numbers which requires to be sorted. (that can't be the problem that's too easy I hear you ask)

However this needs to be sorted in addition with another column that contains order numbers(called order). Most of these however do not contain order numbers.

What I am trying to do is sort the data by the random number that has a number in the order field before eventually filtering down to records that has no order numbers.

so the result table would look a like the below

random order
932333 2313
232332 2331
123123 3233
999322
733232
544324
432131
323133

Any ideas please?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-04 : 10:27:31
Just an idea:
order by random DESC, case when isnull(order,'')='' then 9999 else order end


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2010-01-04 : 10:30:08
That would only sort by the random field, my understanding is that it would only do the second sort by if, the database contained two random numbers that were the same.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-04 : 11:13:43
You are right, sorry.
This should work:

order by
case
when [order] is null then random
else 1000000+random
end
DESC



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -