0

I have a simple select query like below which has only 2 columns:

SELECT a, b FROM table;

I have some mapping data with me like below:

if a=1 and b=1 then c=100
if a=1 and b=2 then c=200
if a=2 and b=1 then c=300
and so on.

Now, I need to create a query so that I can get output like:

1,1,100
1,2,200
2,1,300
and so on

Here I don't want to create a table and store the mapping data. I can create any data structures in PL/SQL to store it.

How can I achieve this?

PS: I tried to create this mapping data using a PL/SQL table and using INNER JOIN. But I realized PL/SQL tables can not be used in SQL queries.

I am using Oracle 11g.

2
  • 1
    What is a "PL/SQL table"? Commented Jul 25, 2013 at 19:46
  • PL/SQL tables can not be used in SQL queries yes they can. You only need to have the global types creation privilege Commented Jul 25, 2013 at 20:37

2 Answers 2

2

You can generate the column c output on-the-fly in your query

SELECT a, b,
       case when a=1 and b=1 then 100 
                 a=1 and b=2 then 200 
                 a=2 and b=1 then 300 
       end as c
FROM table;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use CASE statement to test the value. The purpose of extra WHERE clause on the following query is to speed up the query a little bit since it will only test for values present on the filter condition.

SELECT  a, b,
        CASE WHEN a = 1 AND b = 1 THEN 100
            WHEN a = 1 AND b = 2 THEN 200
            WHEN a = 2 AND b = 1 THEN 300
        END c
FROM    tableName
WHERE   a IN (1,2) AND
        b IN (1,2)

however, if you want to run in all records, you can just remove the WHERE clause.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.