Solved

Using DECODE in a where clause

Posted on 2003-10-29
6
1,959 Views
Last Modified: 2009-05-15
Using DECODE in where clause

I have a where clause that I want to be based on a parameter value. I could have different queries but I thought that I would try DECODE. The other challange is that the decode is in an IN clause. Here is the code.

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(decode ('&GradeName','PreK','1',
'K','(1,2)',
'1','(''1'',''2'')',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'));

This works fine if I enter PreK, 10, 11 or 12, but any other doesn't work. How can I introduce the list of options in the IN clause usint the DECODE THEN portion if I have more than one to return?

I tried two other approaches with 'K', and '1', but they don't work.

Angel

0
Comment
Question by:aleyva
6 Comments
 
LVL 15

Expert Comment

by:andrewst
ID: 9643772
The problem is that "colname IN ('1,2,3')" doesn't have the same menaing as "colname IN (1,2,3)".  The first list has ONE varchar2 element '1,2,3' and the second has THREE elements 1, 2 and 3.

You could rewrite like this:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in (1,2)
      or &GradeName = 2 and CT.trip_grade in (1,2)
      or ... etc. etc.
      )

0
 
LVL 5

Expert Comment

by:FBIAGENT
ID: 9643901
or

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and substr(decode ('&GradeName','PreK','1',
'K','1,2',
'1','1,2',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'),CT.trip_grade,1)>=1;
0
 

Author Comment

by:aleyva
ID: 9643907
What about something like "colname in ('1','2','3')"

How can I get DECODE to return that type of result?

Would the DECODE be faster or the OR?

A
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:andrewst
ID: 9643993
If trip_grade is a varchar2 then:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in ('1','2')
      or &GradeName = 2 and CT.trip_grade in ('1','2')
      or ... etc. etc.
      )

In your original post, you sometimes had (1,2) and sometimes had ('1','2'), which I found odd.  Column trip_grade is either a NUMBER or a VARCHAR2, it can't be both.

As for whether the DECODE or OR is faster (assuming you could get the DECODE to work), I don't suppose it makes any difference: both will cause a full table scan.


0
 
LVL 8

Accepted Solution

by:
Danielzt earned 500 total points
ID: 9643994
Can you try to create a table to save these values? this will make your coding and maintaining easy.

let's you use table grade_mapping
create table grade_mapping
( grade_name varchar2(20), grade number(2))

insert into table grade_mapping
valuse('Prek',1);
insert into table grade_mapping
valuse('K',1);
insert into table grade_mapping
valuse('K',2);
insert into table grade_mapping
valuse('1',1);
insert into table grade_mapping
valuse('1',2);
.....

So your query will be:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(select grade from grade_mapping where grade_name= '&GradeName')

0
 

Author Comment

by:aleyva
ID: 9644757
Well, the reason for the odd coding in my example is because I tried those three different approaches, sorry I wasn't clear on that.

What I am going to consider is the table mapping solution. This will make the maintance much easier because if the mapping changes, then all I have to do is change the data in the database, and not  have to change the Forms code.

Let's see how this works out for me.

A
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now