By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,575 Members | 1,968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,575 IT Pros & Developers. It's quick & easy.

order by for 3 columns

100+
P: 112
hello there,

I have 3 columns which I have to sort.
My data is

-List Item

- Albany 22 194
- Anchorage 24 194
- Canada 38 34
- Carbondale 40 194
- China 51 40
- Des Moines 61 194
- England / UK 307 193
- France 69 66
- Hong Kong 83 84
- Mexico 308 123
- Nashville 119 194
- Russia 310 153
- Singapore 172 165

I want the data to be sorted as

1. Albany 22 194

Anchorage 24 194

Carbondale 40 194

Des Moines 61 194

Nashville 119 194

Canada 38 34

China 51 40

England / UK 307 193

France 69 66

Hong Kong 83 84

Mexico 308 123

Russia 310 153

Singapore 172 165

I tried to use the order by CASE but could not achieve it. can somebody please help me.
thanks in advance.
Dec 16 '09 #1
Share this Question
Share on Google+
4 Replies


ck9663
Expert 2.5K+
P: 2,878
What's the category for sorting?


-- CK
Dec 16 '09 #2

100+
P: 112
the last column has to be in decreasing order and then..first row has to be in alphabetical order..for all the values where column three is not 194, have to be arranged in ascending order.....am I clear?
let me put it in other words, the values of 3rd column have to be sorted as all 194s first and then the rest of them in ascending order....please let me know if i am not clear..
Dec 17 '09 #3

100+
P: 112
thank you CK for the reply..

i got the desired results using union statement..
Dec 17 '09 #4

ck9663
Expert 2.5K+
P: 2,878
you can also try

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT....
  3.  
  4. FROM...
  5.  
  6. WHERE...
  7.  
  8. ORDER BY case when COL3 = 194 then 0 else 1 end,  COL1, COL2
  9.  
  10.  
Happy Coding!!!

--- CK
Dec 17 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.