dbTalk Databases Forums  

Case statement in Order by section?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Case statement in Order by section? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rich
 
Posts: n/a

Default Case statement in Order by section? - 07-24-2008 , 08:03 AM







I am usint 10g.

I know that you can put a decode in the order by section. But I tried
to see if it is possible to put a case statement in the order by
section. My little experiment did not work.

Has anyone in this forum successfully put a case statement in the
order by section? If so, how did you do it. Below is a sample of what
I tried to do.

CREATE OR REPLACE PROCEDURE TestOrderBy
(
User_Choice IN VARCHAR,
TestCur IN OUT Test_OrderBy.TEstCur )
IS

Begin

Open TestCur For
select object_name, User_Choice
from all_objects
Order by
Case
When User_Choice = 'A' Then object_name
Else Object_Name desc
End;

END TestOrderBy;

Reply With Quote
  #2  
Old   
Gints Plivna
 
Posts: n/a

Default Re: Case statement in Order by section? - 07-24-2008 , 09:18 AM






On 24 Jūl., 16:03, Rich <richma... (AT) earthlink (DOT) net> wrote:
Quote:
I am usint 10g.

I know that you can put a decode in the order by section. But I tried
to see if it is possible to put a case statement in the order by
section. My little experiment did not work.

Has anyone in this forum successfully put a case statement in the
order by section? If so, how did you do it. Below is a sample of what
I tried to do.

CREATE OR REPLACE PROCEDURE TestOrderBy
(
*User_Choice * * * * * * *IN * * VARCHAR,
*TestCur * * * *IN OUT Test_OrderBy.TEstCur )
IS

Begin

Open TestCur For
* select object_name, User_Choice
* from all_objects
* Order by
* * Case
* * * * When User_Choice = 'A' Then object_name
* * * Else Object_Name desc
* * End;

END TestOrderBy;
You CAN put case statement as follows:

SQL> select city from table1
2 order by case when length(city) >8 then 1
3 when length(city) <5 then 2
4 else 3
5 end
6 /

CITY
----------
STOCKHOLM
RIGA
RIGA
RIGA
HELSINKI
HELSINKI
VILNIUS
TALLINN
TALLINN
TALLINN

But you are doing more you want query to be dynamic i.e. either order
it asc or desc based on user_choice. Your query doesn't make sense
because you are somehow trying to add asc or desc for each returned
row. So essentially you can simply add if condition and based on
user_choice return one or another cursor.

Gints Plivna
http://www.gplivna.eu


Reply With Quote
  #3  
Old   
Gints Plivna
 
Posts: n/a

Default Re: Case statement in Order by section? - 07-24-2008 , 09:18 AM



On 24 Jūl., 16:03, Rich <richma... (AT) earthlink (DOT) net> wrote:
Quote:
I am usint 10g.

I know that you can put a decode in the order by section. But I tried
to see if it is possible to put a case statement in the order by
section. My little experiment did not work.

Has anyone in this forum successfully put a case statement in the
order by section? If so, how did you do it. Below is a sample of what
I tried to do.

CREATE OR REPLACE PROCEDURE TestOrderBy
(
*User_Choice * * * * * * *IN * * VARCHAR,
*TestCur * * * *IN OUT Test_OrderBy.TEstCur )
IS

Begin

Open TestCur For
* select object_name, User_Choice
* from all_objects
* Order by
* * Case
* * * * When User_Choice = 'A' Then object_name
* * * Else Object_Name desc
* * End;

END TestOrderBy;
You CAN put case statement as follows:

SQL> select city from table1
2 order by case when length(city) >8 then 1
3 when length(city) <5 then 2
4 else 3
5 end
6 /

CITY
----------
STOCKHOLM
RIGA
RIGA
RIGA
HELSINKI
HELSINKI
VILNIUS
TALLINN
TALLINN
TALLINN

But you are doing more you want query to be dynamic i.e. either order
it asc or desc based on user_choice. Your query doesn't make sense
because you are somehow trying to add asc or desc for each returned
row. So essentially you can simply add if condition and based on
user_choice return one or another cursor.

Gints Plivna
http://www.gplivna.eu


Reply With Quote
  #4  
Old   
Gints Plivna
 
Posts: n/a

Default Re: Case statement in Order by section? - 07-24-2008 , 09:18 AM



On 24 Jūl., 16:03, Rich <richma... (AT) earthlink (DOT) net> wrote:
Quote:
I am usint 10g.

I know that you can put a decode in the order by section. But I tried
to see if it is possible to put a case statement in the order by
section. My little experiment did not work.

Has anyone in this forum successfully put a case statement in the
order by section? If so, how did you do it. Below is a sample of what
I tried to do.

CREATE OR REPLACE PROCEDURE TestOrderBy
(
*User_Choice * * * * * * *IN * * VARCHAR,
*TestCur * * * *IN OUT Test_OrderBy.TEstCur )
IS

Begin

Open TestCur For
* select object_name, User_Choice
* from all_objects
* Order by
* * Case
* * * * When User_Choice = 'A' Then object_name
* * * Else Object_Name desc
* * End;

END TestOrderBy;
You CAN put case statement as follows:

SQL> select city from table1
2 order by case when length(city) >8 then 1
3 when length(city) <5 then 2
4 else 3
5 end
6 /

CITY
----------
STOCKHOLM
RIGA
RIGA
RIGA
HELSINKI
HELSINKI
VILNIUS
TALLINN
TALLINN
TALLINN

But you are doing more you want query to be dynamic i.e. either order
it asc or desc based on user_choice. Your query doesn't make sense
because you are somehow trying to add asc or desc for each returned
row. So essentially you can simply add if condition and based on
user_choice return one or another cursor.

Gints Plivna
http://www.gplivna.eu


Reply With Quote
  #5  
Old   
Gints Plivna
 
Posts: n/a

Default Re: Case statement in Order by section? - 07-24-2008 , 09:18 AM



On 24 Jūl., 16:03, Rich <richma... (AT) earthlink (DOT) net> wrote:
Quote:
I am usint 10g.

I know that you can put a decode in the order by section. But I tried
to see if it is possible to put a case statement in the order by
section. My little experiment did not work.

Has anyone in this forum successfully put a case statement in the
order by section? If so, how did you do it. Below is a sample of what
I tried to do.

CREATE OR REPLACE PROCEDURE TestOrderBy
(
*User_Choice * * * * * * *IN * * VARCHAR,
*TestCur * * * *IN OUT Test_OrderBy.TEstCur )
IS

Begin

Open TestCur For
* select object_name, User_Choice
* from all_objects
* Order by
* * Case
* * * * When User_Choice = 'A' Then object_name
* * * Else Object_Name desc
* * End;

END TestOrderBy;
You CAN put case statement as follows:

SQL> select city from table1
2 order by case when length(city) >8 then 1
3 when length(city) <5 then 2
4 else 3
5 end
6 /

CITY
----------
STOCKHOLM
RIGA
RIGA
RIGA
HELSINKI
HELSINKI
VILNIUS
TALLINN
TALLINN
TALLINN

But you are doing more you want query to be dynamic i.e. either order
it asc or desc based on user_choice. Your query doesn't make sense
because you are somehow trying to add asc or desc for each returned
row. So essentially you can simply add if condition and based on
user_choice return one or another cursor.

Gints Plivna
http://www.gplivna.eu


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.