Query (select + insert with case)

Hi All,
I have a table like as shown below:
[code]
SQL> select * from v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> conn scott/tiger
Connected.
SQL> select * from dept;
    DEPTNO DNAME          LOC
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
SQL> create table test(deptno number,id number,name varchar(20),salary number);
Table created.
SQL> alter table test add constraint ref_deptno foreign key (deptno) references dept(deptno);
Table altered.
SQL> insert into test values(10,1,'clark',10);
1 row created.
SQL> insert into test values(20,1,'clark',15);
1 row created.
SQL> insert into test values(10,2,'jones',13);
1 row created.
SQL> insert into test values(20,3,'king',25);
1 row created.
SQL> insert into test values(10,3,'king',25);
1 row created.
SQL> insert into test values(10,4,'ford',35);
1 row created.
SQL> insert into test values(20,4,'ford',35);
1 row created.
SQL> insert into test values(10,5,'scott',50);
1 row created.
SQL> insert into test values(20,6,'miller',50);
1 row created.
SQL> insert into test values(10,7,'smith',50);
1 row created.
SQL> select t.*,d.dname from test t join dept d on(d.deptno=t.deptno) order by t.id;
    DEPTNO         ID NAME                     SALARY DNAME
        10          1 clark                        10 ACCOUNTING
        20          1 clark                        15 RESEARCH
        10          2 jones                     13 ACCOUNTING
        20          3 king                         25 RESEARCH
        10          3 king                         25 ACCOUNTING
        10          4 ford                         35 ACCOUNTING
        20          4 ford                         35 RESEARCH
        10          5 scott                        50 ACCOUNTING
        20          6 miller                       50 RESEARCH
        10          7 smith                        50 ACCOUNTING
10 rows selected.
[code]
I want o/p should be like:
{code}
      ID NAME                     SALARY
      1 clark Accounting           10
      1 clark                             15
      2 jones Accounting           13
   2 jones      
                     0
      3 king                              25
      3 king Accounting             25
      4 ford Accounting              35
      4 ford                               35
      5 scott  Accounting          50
  5 scott                           
0
      6 miller                             50
      7 smith Accounting           50
  7 smith                            0
{code}
Logic:
It is group by id.
1- When corresponding to a id, we have names with dname as Accounting and research, then we have to print name +accounting and name.
{code}like: name       dname          sal                     o/p should be like:          name                      sal
               clark        accounting      10                                                         clark accounting       10
               clark         research         15                                                         clark                        15
{code}
2- If there is only one name and its associated with accounting department, then we have to print name + accounting and one extra row with same name with sal=0.
{code}like: name       dname           sal                    o/p should be like:          name                       sal
               jones        accounting       13                                                      jones accounting        13
                                                                                                              jones                         0
{code}
3- If there is only one name and its associated with research department, then we have to print name only.
{code}like:  name        dname       sal                     o/p should be like:           name                    sal
                 miller         research    50                                                           miller                     50
{code}

Hi,
my fault as I haven't read well the requirements.
Here is the modified version:
WITH got_cartesian AS
   SELECT DISTINCT t.id, t.name, d.deptno, d.dname
     FROM test t CROSS JOIN dept d
    WHERE d.deptno IN (10, 20)
SELECT c.id, c.name||CASE WHEN c.deptno=10 THEN ' Accounting' END name , NVL (t.salary, 0) sal, c.dname
  FROM got_cartesian c LEFT OUTER JOIN test t
       ON (c.id = t.id AND c.deptno = t.deptno)
WHERE c.deptno!=10 OR t.salary IS NOT NULL
ORDER BY c.id, c.deptno;
Output will be like this:
ID NAME                              
SAL DNAME   
1 clark Accounting                   
10 ACCOUNTING   
1 clark                              
15 RESEARCH
2 jones Accounting                   
13 ACCOUNTING   
2 jones                               
0 RESEARCH
3 king Accounting                    
25 ACCOUNTING   
3 king                               
25 RESEARCH
4 ford Accounting                    
35 ACCOUNTING   
4 ford                               
35 RESEARCH
5 scott Accounting                   
50 ACCOUNTING   
5 scott                               
0 RESEARCH
6 miller                             
50 RESEARCH
7 smith Accounting                   
50 ACCOUNTING   
7 smith                               
0 RESEARCH  
Regards.
Al

Similar Messages

  • Query in expdp with case-sensitive table and columnname

    Hi,
    I've got a problem with exporting some specific rows of a table.
    The problem is a case-sensitive table and column name. I've tried this in a parfile:
    dumpfile=dpumpdir1:test.dmp
    logfile=dpumpdir1:test.log
    tables="Data"
    query="Data":'"where "DataID" = 11"'
    but it ended like this:
    ORA-31693: Table data object "Data" failed to load/unload and is being skipped due to error:
    ORA-06502: PL/SQL: numeric or value error
    ORA-31605: the following was returned from LpxXSLSetTextVar in routine kuxslSetParam:
    LPX-314: an internal failure occurred
    Edited by: [Logik on 18.04.2013 01:18                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    I've tried to make a function with this. I think you used some Oracle 11 parameters. reusefile for example.
    However I've managed to compile the function without error:
    CREATE OR REPLACE FUNCTION EXPDP RETURN NUMBER IS
      h1 number;
      v_job_state       varchar2(4000);
    BEGIN
      h1 := dbms_datapump.open(operation=>'EXPORT',job_mode=>'TABLE',job_name=>'HARRY10');
      dbms_datapump.add_file(h1,'example1.dmp','DPUMPDIR1');
      dbms_datapump.add_file(h1,'example1.log','DPUMPDIR1',filetype => dbms_datapump.ku$_file_type_log_file);
      dbms_datapump.metadata_filter(handle => h1,name => 'NAME_EXPR',value => 'IN (''Data'')',object_type => 'TABLE');
      dbms_datapump.data_filter(handle => h1,name => 'SUBQUERY',value => 'WHERE "DataID" = 1');
      dbms_datapump.start_job(h1);
      DBMS_DATAPUMP.WAIT_FOR_JOB (h1,v_job_state);
      DBMS_OUTPUT.PUT_LINE(v_job_state);
      RETURN NULL;
    END EXPDP;But when I execute I get:
    ORA-31626: job does not exist
    ORA-06512: at "SYS.DBMS_SYS_ERROR", line 79
    ORA-06512: at "SYS.DBMS_DATAPUMP", line 938
    ORA-06512: at "SYS.DBMS_DATAPUMP", line 4592
    ORA-06512: at EXPDP", line 5
    ORA-06512: at line 5
    Probably not my day :(
    By the way... how can I Format the text as a script?
    Edited by: [Logik on 19.04.2013 11:01                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Performance issues with respect scheme registration,select & insert query

    I am facing performance issues with respect to schema registration,Select & insert query towards 10.2.0.3 version.It is taking around 45 minutes to register schema and it is taking around 5 min to insert a single document into xml db where as it was taking less than min to insert a single document into xml db of 9.2.0.6 version.Would like to know the issue and solution to resolve this issue.Please help me out on this as it is very urgent for me

    Since it appears that this is an XML DB specific question, you're probably better off posting in the XML DB. The folks over there have much more experience with the ins and outs of that particular product.
    Justin

  • Select list with redirect and save the item to insert row

    On the Form, I want to select from the item (P105_ADMIN_DOMAIN_ID)
    and pop the result on the item (P105_ADMIN_NAME).
    P105_ADMIN_DOMAIN_ID define:
    Display as "select list with redirect"
    Alternate source used "Only when .."
    Source Type "Database Column"
    Source Value or express "ADMIN_DOMAIN_ID"
    P105_ADMIN_NAME define:
    Display as "Text Field"
    Altrenate source used "Only when .."
    Source Type "SQL Query"
    Source Value or express "select party
    from responsible_party
    where logonid
    = :P105_ADMIN_DOMAIN_ID"
    questions:
    1. item P105_ADMIN_DOMAIN_ID DROP DOWN LIST is limited to
    some threadhold, it did not list all the value from
    the LOV which is follow:
    select LOGONID d, LOGONID r
    from RESPONSIBLE_PARTY
    order by 1
    2. I want to insert a row after complete the form.
    But my item P105_ADMIN_NAME is defined
    Source Type "SQL Query" instead of the 'Database
    Column'
    How could I overcome these?
    Thanks so much for your help.

    1) what i meant was for you to test this ridiculously huge LOV outside of htmldb, but it occurred to me last night that you could, in fact, be hitting a limit of ours. after some poking around, i have confirmed that. select list items in htmldb are currently limited to 10,000 rows. this was initially done for performance reasons, but they're now opening up the restriction. if you really need to work with that many rows where the user is allowed to pick from a set of values, consider using a popup item type instead. it'd help your app performance as well (in my test cases, it stank to have to wait for my 10k select list rows to come down to my browser).
    2) sorry to not have been more clear about this, but when you set your item's "Source Type" back to "Database Column", you'd have to specify that column in the "Source or value expression" field. also, you can't put a sql query into that "Post Calculation Computation" field. as i said before, "you can take a look at the attribute-level help for that field to see implementation examples."
    3) stick with "Always..." for now if you're using our auto-dml process(es)
    regards,
    raj

  • Make this select query to Select Insert query

    Hi,
    How to make this select query to select Insert query..
    ;WITH DateDim AS
    SELECT CAST('1900-01-01' AS DATETIME) StartDate
    UNION ALL
    SELECT StartDate + 1
    FROM DateDim
    WHERE StartDate + 1 <= '2099-12-31'
    SELECT CAST(CONVERT(CHAR(8),CAST(StartDate AS DATETIME),112) AS INT) AS DateKey
    , StartDate
    , DATEPART(dw, StartDate) AS DayNumberOfWeek
    , DATENAME(dw, StartDate) AS EnglishDayNameOfWeek
    , DAY(StartDate) AS DayNumberOfMonth
    , (CASE WHEN MONTH(StartDate)>=1 AND MONTH(StartDate) <=6 THEN 1 ELSE 2 END) AS CalendarSemester
    FROM DateDim 
    OPTION (MAXRECURSION 0) 
    GO

    The final solution is                            
     ;WITH DateDim AS
    SELECT CAST('1900-01-01' AS DATETIME) StartDate
    UNION ALL
    SELECT StartDate + 1
    FROM DateDim
    WHERE StartDate + 1 <= '2099-12-31'
    SELECT CAST(CONVERT(CHAR(8),CAST(StartDate AS DATETIME),112) AS INT) AS DateKey
    , StartDate
    , DATEPART(dw, StartDate) AS DayNumberOfWeek
    , DATENAME(dw, StartDate) AS EnglishDayNameOfWeek
    , DAY(StartDate) AS DayNumberOfMonth
    , (CASE WHEN MONTH(StartDate)>=1 AND MONTH(StartDate) <=6 THEN 1 ELSE 2 END) AS CalendarSemester
    Into new_tablenameFROM
    DateDim 
    OPTION (MAXRECURSION 0) 
    GO

  • Help in query required – Insert, Select in same table

    Hi All
    I need your help on writing the queries effectively.
    Oracle Version: 10.2.0.3.0
    OS: UNIX
    I have a table METRICS_TBL as mentioned below.
    CYCLE_DATE     METRICS     VALUE
    08/17/2008     COST-TV     100
    08/17/2008     COST-NEWSPAPER     50
    08/17/2008     COST-POSTALMAIL     25
    08/17/2008     PROD-TV     10
    08/17/2008     PROD-NEWSPAPER     25
    08/17/2008     PROD-POSTALMAIL     5
    Based on the above data, I need to append (Insert into METRICS_TBL select from METRICS_TBL) the same table with the records as mentioned below.
    08/17/2008     COSTPERPROD-TV     10
    08/17/2008     COSTPERPROD-NEWSPAPER     2
    08/17/2008     COSTPER PROD-POSTALMAIL     5
    Basically, I need to calculate Cost per Product for each category. Depending upon the available metrics, metrics also should be changed like COSTPERPROD and values should be Cost/prod under each category.
    Can somebody help me with the query.
    Thanks

    SQL> WITH t AS
      2       (
      3          SELECT TO_DATE('8/17/2008', 'MM/DD/YYYY') AS CYCLE_DATE, 'COST-TV' AS METRICS, 100 AS VALUE
      4            FROM DUAL
      5          UNION ALL
      6          SELECT TO_DATE('08/17/2008', 'MM/DD/YYYY'), 'COST-NEWSPAPER', 50
      7            FROM DUAL
      8          UNION ALL
      9          SELECT TO_DATE('08/17/2008', 'MM/DD/YYYY'), 'COST-POSTALMAIL', 25
    10            FROM DUAL
    11          UNION ALL
    12          SELECT TO_DATE('08/17/2008', 'MM/DD/YYYY'), 'PROD-TV', 10
    13            FROM DUAL
    14          UNION ALL
    15          SELECT TO_DATE('08/17/2008', 'MM/DD/YYYY'), 'PROD-NEWSPAPER', 25
    16            FROM DUAL
    17          UNION ALL
    18          SELECT TO_DATE('08/17/2008', 'MM/DD/YYYY'), 'PROD-POSTALMAIL', 5
    19            FROM DUAL)
    20  SELECT COST.CYCLE_DATE, 'COSTPERPROD-' || SUBSTR(COST.metrics, 6) AS Metrics,
    21         COST.VALUE / prod.VALUE AS COSTPERPROD
    22    FROM t COST, t prod
    23   WHERE COST.CYCLE_DATE = PROD.CYCLE_DATE
    24     AND COST.metrics LIKE 'COST-%'
    25     AND prod.metrics LIKE 'PROD-%'
    26     AND SUBSTR(COST.metrics, 6) = SUBSTR(prod.metrics, 6)
    27  /
    CYCLE_DA METRICS                                    COSTPERPROD
    17.08.08 COSTPERPROD-NEWSPAPER                                2
    17.08.08 COSTPERPROD-POSTALMAIL                               5
    17.08.08 COSTPERPROD-TV                                      10

  • Help on converting query with case into decode

    Hi Can anyone help me to revise my plsql query? Below is a portion og the procedure I am changing to meet Oracle 8i.
    SELECT SUM(NVL(CASE WHEN (weeks='wk1' ) THEN qty END, 0)) wk1,
    SUM(NVL(CASE WHEN (weeks='wk2' ) THEN qty END, 0)) wk2,
    SUM(NVL(CASE WHEN (weeks='wk3' ) THEN qty END, 0)) wk3,
    SUM(NVL(CASE WHEN (weeks='wk4' ) THEN qty END, 0)) wk4
    FROM TABLE1
    Thanks in advance.

    pls try this....(code not tested as no create table etc provided).
    select dt
    from (select decode(is_tbd,
    'yes',
    decode(is_tbd_order,
    'no',
    'tbd',
    decode(sign(ex_fac_date - (v_asofdate + 131)),
    1,
    'tbd',
    null),
    decode(sign(ex_fac_date - (v_asofdate + 131)),
    1,
    'tbd',
    null)),
    null) dt
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate - 1)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 5)),
    0,
    decode(is_tbd, 'no', 'wk1', null),
    null),
    null)
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate + 6)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 12)),
    0,
    decode(is_tbd, 'no', 'wk2', null),
    null),
    null)
    from dual
    select decode(sign(ex_fac_date - (v_asofdate + 13)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 19)),
    0,
    decode(is_tbd, 'no', 'wk3', null),
    null),
    null)
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate - 1)),
    0,
    decode(is_tbd, 'no', 'past_due', null),
    null)
    from dual
    where dt is not null
    Regards

  • Query variable : selection option with no interval

    Hello,
    Is it possible to define a query variable as selection option, with no interval (like in abap).
    I am in 3.X version.
    Many Thanks in advance,
    Manuel

    Hi Manuel,
    This should be possible through multiple single value variables, but to ensure that the user has not in input the same charateristic value for exclusion and selection you may choose to write customer exit at the background.
    I think interval and select option would allow 'To values'.
    So I was wrong in what I said before. SORRY
    However multiple single value would not allow exclusion, I think.
    Your scenario would only be possible by excluding multiple single values in variable type select option and process the variable using customer exit, to trmove all to Values.
    But you are right, you cannot have your scenrio handled , I think.
    ( ie multiple exclusion in from values with no To Values )
    One more way is restrict your characteristic by two input ready variables, one select option for exclusion and have a customer exit to delte all inclusion that were input accidently and one multiple value ready for input variable.
    But, all in all, there would be no straight drive through this I think.
    Hope it helps,
    Regards,
    Sunmit.

  • Insert with Select while Using Merge

    Actual Query
    MERGE INTO schema1.employees D
       USING (SELECT employee_id, salary, department_id FROM schema2.employees
       WHERE department_id = 80) S
       ON (D.employee_id = S.employee_id)
       WHEN MATCHED THEN
       UPDATE
       SET D.bonus = D.bonus + S.salary*.01
       WHERE (S.salary > 8000)
       WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
       VALUES (S.employee_id, S.salary*.01)
       WHERE (S.salary <= 8000) Is there a possible way to write a select Query in Insert Statement while using Merge.
      WHEN NOT MATCHED THEN
       INSERT
       SELECT * FROM schema2.employees
       WHERE S.salary <= 8000;

    it works:
    MERGE INTO fifapps.tes2
    using dual
    on (1=2)
    when not matched then insert values(59,'yes');
    commit;or
    MERGE INTO fifapps.tes2
    using dual
    on (1=2)
    when not matched then insert values ((select 99 from dual),(select'weii' from dual));
    commit;Edited by: ʃʃp on Jun 13, 2012 2:03 AM
    Edited by: ʃʃp on Jun 13, 2012 2:03 AM

  • How to insert with select in table with object types

    I am in the proces of redesigning some tables, as i have upgraded from
    personal oracle 7 to personal oracle 8i.
    I have constructed an object type Address_type, which is one of the columns
    in a table named DestTable.
    The object type is created as follows:
    CREATE OR REPLACE TYPE pub.address_type
    AS OBJECT
    Street1 varchar2(50),
    Street2 varchar2(50),
    ZipCode varchar2(10));
    The table is created as follows:
    CREATE TABLE pub.DestTable
    (id INTEGER PRIMARY KEY,
    LastName varchar2(30),
    FirstName varchar2(25),
    Address pub.address_type);
    Inserting a single row is ok as i use the following syntax:
    Insert into DestTable(1, '******* ', 'Lawrence', pub.address_type(
    '500 Oracle Parkway', 'Box 59510', '95045'));
    When i try to insert values into the table by selecting from another table i
    cannot do it and cannot figure out what is wrong
    I have used the following syntax:
    Insert into DestTable
    id, name, pub.address_type(Street1, Street2, ZipCode))
    select
    id, lastname, firstname, street1, street2, ZipCode
    from SourceTable;
    I have also tried the following syntax:
    Insert into DestTable
    id, name, pub.address_type(Address.Street1, Address.Street2,Address.ZipCode))
    select
    id, lastname, firstname, street1, street2, ZipCode
    from SourceTable;
    What is wrong here ?
    null

    Magnus,
    1. Check out the examples on 'insert with subquery' in http://otn.oracle.com/docs/products/oracle8i/doc_library/817_doc/server.817/a85397/state21b.htm#2065648
    2. Correct your syntax that repeated the column definition after "insert into ..."
    Insert into DestTable
    id, name, pub.address_type(Street1, Street2, ZipCode))
    select
    id, lastname, firstname, street1, street2, ZipCode
    from SourceTable;
    Regards,
    Geoff
    null

  • ORA-20001:Error fetching column value:Query must  begin with SELECT or WITH

    Hi all,
    I have implemented an application with a master-detail form using Apex 3.1.2 and I imported that to Apex 3.2 on different server. Everything else seems to be fine except when I add detail record (clicking on Add Rows), I am getting following error.
    =================
    report error:
    ORA-20001: Error fetching column value: ORA-20001: Query must begin with SELECT or WITH
    ====================
    Both the databases have same version 10gR2. I didn't make any change after importing it as well. Can someone let me know what could be the issue? Thanks in advance.
    SHY

    May we know your first name?
    Can you put the application in your workspace on apex.oracle.com so we can take a look?
    Scott

  • ORA-20001: Query must begin with SELECT or WITH

    Hi,
    On a page we have a hidden item :P3_SQL in which we save the dynamic sql query.
    We have a report region of SQL QUERY type whose source is this item :P3_SQL.
    This used to work fine in apex 3.*. We have upgraded our application to 4.0.1 and now we get this error.
    ORA-20001: Query must begin with SELECT or WITH
    I have a workaround for this, but just wanted to make sure somebody recognizes this as a bug in our latest apex version.
    Thanks
    Knut

    Hello Knut,
    I'm not sure if you encountered a bug or an improved validation of your query. If you had just ":P3_SQL" as source, this obviously is neither a correct query nor is it a function returning a query. I guess your workaround is actually encapsulating ":P3_SQL" in a block that returns the value. This is the intended way, not a bug. You just were lucky it was not validated and worked in previous versions.
    If my assumption was wrong, please post both working and non-working solution and a more precise description on when this error actually occurs.
    Thanks,
    Udo

  • Using select query to insert into a field

    Hi,
    I am working with a jdbc-to-jdbc scenario, in which on source side iam geting data from 2 tables t1 & t2 and inserting this data into two tables tb1 & tb2 on target side.A field in tb2 has to be inserted with a value which is equal to the number of records inserted into the table tb1 i.e with the number equal to 'how many records from the table t1 has been retrieved inorder to insert into table tb1'.
    Can anybody please suggest me.....
    Thanks & Regards,
    Radhika.

    consider ur strucutre
    <Data>
    <id> 1 </id> 0..1
    <id> 2 </id>
    <id> 3 </id>
    <id> 4 </id>
    </Data>
    here i meant context to <id>. Mapping id > count > target will give the nunmber of records in Data i.e. 4
    note: here u have change the context of <id> to <data>
    Message was edited by:
            Prabhu  S

  • Using a Select list with Submit

    I am trying to use the select list with Submit on my page.
    I have 3 select lists.
    On the first one,I am showing the list of all jacks.
    When the user selects a jack from the first list, I want to show all the jacks except the one which was selected.
    When the user selects a jack from the second list, I want to show all the jacks except the ones selected in the first list and the second list.
    I have created the select lists with submit and then I have put the query for getting the jacks. I created an unconditional branch. I am also passing the variables so that I can set the values of the items on submit.
    I also created a computation as mentioned in one of the OTN discussions, and have mentioned the value of second list to be calculated from the first and 3rd from first and second.
    But after clicking the first select list, I am losing all the existing values on the first page. I wonder what I am missing here. Can anyone hellp?
    Thanks,
    Gargi

    Gargi,
    In this case using 'select list with redirect' makes more sense rather than with SUBMIT. So change all the select lists to 'select list with redirect' (it redirects to same page inserting 'select list' value session state).
    Now for the 2nd select list query add a condition in where clause like
    WHERE sl_col != :P1_SL1And for the 3rd select list query add a condition in where clause like
    WHERE sl_col NOT IN (:P1_SL1, :P1_SL2)Hope it helps :-)
    Cheers,
    Hari

  • Sql query for emp with decode

    How to get the required output with single query to acheive IF SAL>2500 and job='manager' then display job as 'seniormanager'.
    CREATE TABLE EMP(EMPNO NUMBER(4) NOT NULL,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7, 2),COMM NUMBER(7, 2),DEPTNO NUMBER(2));
    INSERT INTO EMP VALUES(7521, 'WARD', 'SALESMAN', 7698,TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
    INSERT INTO EMP VALUES(7566, 'JONES', 'MANAGER', 7839,TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
    INSERT INTO EMP VALUES(7698, 'BLAKE', 'MANAGER', 7839,TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
    INSERT INTO EMP VALUES(7782, 'CLARK', 'MANAGER', 7839,TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
    INSERT INTO EMP VALUES(7788, 'SCOTT', 'ANALYST', 7566,TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
    IF SAL>2500 and job='manager' then display job as 'seniormanager'
    SELECT * FROM EMP
    expected output
    7521     WARD     SALESMAN         7698     22-FEB-81     1250     500     30
    7566     JONES     SENIORMANAGER     7839     02-APR-81     2975          20
    7698     BLAKE     SENIORMANAGER     7839     01-MAY-81     2850          30
    7782     CLARK     MANAGER          7839     09-JUN-81     2450          10
    7788     SCOTT     ANALYST           7566     09-DEC-82     3000          20

    Hi,
    The answer to "How do I ... using DECODE?", no matter how you fill in the blank, is "Don't use DECODE; use CASE instead." If you have to ask, then it's too complicated to do with DECODE.
    Here's one way using CASE:
    SELECT       empno, ename
    ,       CASE
               WHEN  job = 'MANAGER'
               AND   sal > 2500
               THEN  'SENIORMANAGER'
               ELSE  job
           END     AS d_job
    ,       mgr, hiredate, sal, comm, deptno
    FROM       emp
    ;If you really, really, really want to use DECODE:
    SELECT       empno, ename
    ,       DECODE ( job
                 , 'MANAGER'
               , DECODE ( SIGN (sal - 2500)
                        , 1
                     , 'SENIORMANAGER'
                     , job
               , job
               )     AS d_job
    ,       mgr, hiredate, sal, comm, deptno
    FROM       emp
    ;Nothing is literally too difficult to do in DECODE. Anything that case be done with CASE can also be done using DECODE, often using several times the amount of code. Bit which would you rther maintain: the CASE soltuion I posted first, of this DECODE soltuion. Which do you think will have more bugs? Which so you think will be faster? If you're using Oracle 8.1 (or higher) then don't use inefficient, high-maintenance code like this. Use CASE instead.
    Edited by: Frank Kulash on Dec 26, 2012 3:27 PM
    Added DECODE solution

Maybe you are looking for