MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Order Sequence in a Query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
arvibala
Beginner


Joined: 12 Feb 2008
Posts: 142
Topics: 67

PostPosted: Fri Sep 24, 2010 7:46 am    Post subject: Order Sequence in a Query Reply with quote

I have a table from which I need to extract data for a report. Only Order sequence to pull data using a query that I know of is Ascending or Descending. Can I be helped with a better query so that I can order my results in my way other than Ascending or Descending ?

For example I have a column (OPTION) which can have only below data

Code:

A
E
F
X
Z


I want the records to be sorted on this but in below order using a query.

Code:

A
Z
F
E
X


I heard we can do this in Oracle, but can we in DB2?
[/code]
_________________
Arvind
"You can make a difference with your smile. Have that with you always"
Back to top
View user's profile Send private message Yahoo Messenger
Sqlcode
Intermediate


Joined: 15 Dec 2006
Posts: 157
Topics: 38

PostPosted: Fri Sep 24, 2010 9:32 am    Post subject: Reply with quote

Arvind,
Quote:
Only Order sequence to pull data using a query that I know of is Ascending or Descending.

Please explain the rules to get the expected output? How do you get Z after A and then F?

Thanks,
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Sep 24, 2010 10:39 am    Post subject: Reply with quote

arvibala,

If you just want to change the order for some of the characters, then you can use a case statement and assign some sorted values and use that in order by.

Here is an untested SQL
Code:

SELECT A.COL1                                       
 FROM (SELECT COL1 AS COL1                           
             ,CASE WHEN COL1 = 'Z' THEN CHAR('B')   
                   WHEN COL1 = 'F' THEN CHAR('C')   
                   ELSE COL1 END AS SEQ             
         FROM your_table) A                           
 ORDER BY A.SEQ                                     
 ;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group