RE: conditional 'order by'

  • From: "Kenneth Naim" <kennaim@xxxxxxxxx>
  • To: <eugene.pipko@xxxxxxxxxxxx>, <oracle-l@xxxxxxxxxxxxx>
  • Date: Fri, 19 Dec 2008 12:46:20 -0500

Just use a case statement.

 

Order by case when substr(code,2,1)='F' then decode(code,'F',0,'H',1,2)

                              When  substr(code,2,1)='H' then
decode(code,'H',0,'F',1,2)

Else .

End 

 

 

  _____  

From: oracle-l-bounce@xxxxxxxxxxxxx [mailto:oracle-l-bounce@xxxxxxxxxxxxx]
On Behalf Of Eugene Pipko
Sent: Friday, December 19, 2008 11:57 AM
To: 'oracle-l@xxxxxxxxxxxxx'
Subject: conditional 'order by' 

 

Hi all,

I have a query that needs to be ordered by based on the second letter of the
code passed in it.

For instance if the code is '9F9Q' then I'd order by 'F' , then by 'H', then
by any other.

If the code is '7H7Q' then I'd order by 'H', then by 'F', then by any other.

I know that I can do it in 2 steps, but is it possible to do in one sql
statement?

 

Select .

From .

Where ..

If substr(code,2,1)='F' then

                Order by decode(code,'F',0,'H',1,2);

Elsif substr(code,2,1)='H' then

                Order by decode(code,'H',0,'F',1,2);

End if;

 

 

Thanks,

 

 

Eugene 

P  Please consider the environment before printing this e-mail.

 

Other related posts: