If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 sorting the numbers by default

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-04, 13:42
rameshb241 rameshb241 is offline
Registered User
 
Join Date: Apr 2003
Posts: 6
Question DB2 sorting the numbers by default

Hi, I am running a simple select query to get some numbers back like the following.

select * from TABLE where NUMBER in ('0334','0980','0116','0633','0116D','0115','0114' ,'0141','0635','8371','0240','0142','0316','2478', '0913','0108','0982','0731','0147','2460').

I am expecting the results in the order the IDs are mentioned in the query. But DB2 is by default sorting the number in the resultset. So the first record is showing as 0108 instead of 0334. Is there any way I can turn-off this default ordering of numbers.

Thanks in advance for your help.
Reply With Quote
  #2 (permalink)  
Old 08-20-04, 13:45
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The only way that I know to change the order of a result set is to use the ORDER BY clause. You'll need to get a bit tricky to get the apparently arbitrary order you want, but you can do it using a CASE statement in the ORDER BY clause.

-PatP
Reply With Quote
  #3 (permalink)  
Old 08-20-04, 14:36
rameshb241 rameshb241 is offline
Registered User
 
Join Date: Apr 2003
Posts: 6
Question

I think CASE can be used to substitute something in the results based on some condition. But in my case, I just need to display the numbers in the order it was given is the Query. I am still trying to find some solution.
Thanks again!
Reply With Quote
  #4 (permalink)  
Old 08-20-04, 15:44
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Try something like this...

select .... ,
case NUMBER
WHEN '0334' THEN 1
WHEN '0980' THEN 2 ....
...WHEN '2460' THEN 20
end as NUM_ORDER
from TABLE
where NUMBER in ('0334','0980','0116','0633','0116D','0115','0114' ,'0141','0635','8371','0240','0142','0316','2478', '0913','0108','0982','0731','0147','2460')
order by NUM_ORDER
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On