Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > More about ORDER BY for a specified sequence
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

More about ORDER BY for a specified sequence

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 13 March 2007

How could I sort a query result according to a specific order (not ascending or descending) that I will be supplying from the values of the column to be sorted?



Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

Oracle and SQL
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to use the Oracle Database SQL Reference Manual
How to use SQL Developer to run SQL statements
How to work with the Oracle database home page
How to use SQL*Plus in Oracle
How to use SQL Developer to work with an Oracle database
How to view and edit table column definitions
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
Oracle and SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


One technique for this was described in detail in a previous answer, ORDER BY a specified sequence (21 April 2006):

order 
    by case when projectid = 3 then 'Curly'
            when projectid = 1 then 'Larry'
            when projectid = 2 then 'Moe'
          else null end 

In summary, each row of the intermediate result set has an additional column appended to it, consisting of the result of the CASE expression. This additional column is not returned (because it isn't in the SELECT list, although it could be, if so desired, which you might do during testing). These values are chosen carefully (if not, as in the above example, capriciously), so that they achieve the desired sequencing.

Here's another method. Suppose we have data containing only the three-character month names Jan, Feb, Mar, Apr.

create table mths
( mth char(3) not null primary key 
);
insert into mths
values ('Apr'),('Feb'),('Jan'),('Mar')
;

Now we want to sort these names into their natural calendric sequence.

select mth
     , case when mth='Jan' then 0 else 1 end as m1
     , case when mth='Feb' then 0 else 1 end as m2
     , case when mth='Mar' then 0 else 1 end as m3
     , case when mth='Apr' then 0 else 1 end as m4
  from mths
order 
    by case when mth='Jan' then 0 else 1 end
     , case when mth='Feb' then 0 else 1 end
     , case when mth='Mar' then 0 else 1 end
     , case when mth='Apr' then 0 else 1 end

Ordinarily, you would not include the ORDER BY columns in the SELECT like that. They are included here so that you can see how they work. The result of the above query is:

mth m1 m2 m3 m4
Jan  0  1  1  1
Feb  1  0  1  1
Mar  1  1  0  1
Apr  1  1  1  0

As mentioned, those four "extra" columns would normally not be included in the SELECT clause, they would just be in the ORDER BY. Can you see how they work?

Here is a third solution:

select mth
     , position(mth in 'JanFebMarApr') as p
  from mths
order 
    by position(mth in 'JanFebMarApr')

This query produces:

mth  p
Jan  1
Feb  4
Mar  7
Apr 10

Neat, eh?




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts