Advice on decode or case

Hi
I'm pretty new to SQL and need some help with SQL syntax.
I am trying to write a sql query which takes as input a variable and dependent upon the value of this variable queries for specific values from the database. E.g.,
if varA =x then query d/b for values y1, y2, y3
elsif varA =c then query d/b for values d1, d2
else blah
end
So, I've attempted to write the sql within a decode but I am getting and error (ANELVA invalid identifier).
(input variable below is vartoolset so I'm saying that if vartoolset=Anelva then I query for ANL_51 & ANL_52 from job_history table)
select facility_code,
decode (vartoolset,"Anelva", (select distinct(facility_code) from job_history where facility_code='ANL_51' OR facility_code='ANL_52'),
"GMR", (select facility_code from job_history where facility_code='CVC_81' OR facility_code='CVC_82'),
'blah')
from job_history
Thanks in advance for any help/advice
Emma

The possible values which may be assigned to
vartoolset are:
Anelva
CVC
GMR
602G
If vartoolset has a value of Anelva then I want to
create a list of the following tools onlys:
ANL_51, ANL_52,ANL_53, ANL_54
If vartoolset has a value of CVC then I want to
create a list of the following tools only:
CVC_51,CVC_52,CVC_54 etc
The tools above are being pulled from column called
facility_code in table job_history.
Does this clarify?
ThanksNot clear..
This?
sql>select * from  job_history;
VARTOOLSET FACILITY_CODE
Anelva  ANL_51 
Anelva  ANL_52 
Anelva  ANL_53 
Anelva  ANL_54 
Anelva  CVC_51 
CVC  CVC_51 
CVC  CVC_52 
CVC  CVC_53 
CVC  CVC_54 
CVC  ANL_51 
GMR  ANL_51 
GMR  CVC_51 
SQL>
select facility_code,vartoolset
from job_history
where (vartoolset,facility_code) in (('Anelva','ANL_51'),('Anelva','ANL_52'),('CVC','CVC_51'),('CVC','CVC_52'));
FACILITY_C VARTOOLSET 
ANL_51  Anelva 
ANL_52  Anelva 
CVC_51  CVC 
CVC_52  CVC                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Diff between decode and case

    Hi
    I need small clarification about difference between decode and case
    Thanks who visit my thread

    And for those people who can't be ar$ed to follow links...
    Decode allows for conditional output where the conditions are straightforward matches between values.
    Case allows for regular conditions to be used such as greater than, less than etc.
    Some of the things done by case can be achieved through decode as well but in a more convoluted manner than simply using case.
    ;)

  • Decode Vs Case Statement

    What will be good for proformance wise:
    Decode or Case in a sql statement.?????

    See the following link for Tom Kyte's opinion (point #4 in his first answer):
    http://asktom.oracle.com/pls/ask/f?p=4950:8:16717708356827415201::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:1243867216406

  • Which is the best decode or case

    Hi,
    When you check performance wise which is the best one decode or case?
    Thanks,

    > You mean CPU processor speed or oracle buffer(SGA).
    Neither. CPU architecture. RISC vs CISC vs ..
    On a PA-RISC1 CPU a DECODE is just a tad faster than a CASE. On an AMD64 CPU, the reverse is true.
    > When I increase memory, The case and decode performance will increase?
    No. A CASE and a DECODE does not need memory to work faster. It is a set of machine code instructions that needs to compare values to determine a result. It depends on just how fast the CPU can execute this set of machine code instructions.
    A faster CPU will make a very significant difference. An AMD64 Opteron CPU is a couple of times faster than a PA-RISC1 CPU.
    I had this exact same conversation back in 2006 on this forum - and posted [url
    http://forums.oracle.com/forums/thread.jspa?messageID=1346165&#1346165]this benchmark to show that the decision of using CASE or DECODE is not a decision that should be based on raw performance.

  • Without Decode or Case

    I have emp_allocation table. It has data as below
    EMPID     YEAR      MONTH
    X     2006     JAN
    X     2006     MAR
    Y     2006     JAN
    Y     2006     FEB
    Y     2006     MAR
    I want SQL Query(Without Decode or Case) which will give output as below
    EMPID     YEAR      JAN     FEB     MAR      APR     JUN     JUL
    X     2006     Y     N     Y
    Y     2006     Y     Y     Y

    Why you'd want to do it this way I do not know, but if you insist...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t AS (select 'X' AS EMPID, 2006 AS YEAR, 'JAN' AS MONTH FROM DUAL UNION ALL
      2             select 'X', 2006, 'MAR' FROM DUAL UNION ALL
      3             select 'Y', 2006, 'JAN' FROM DUAL UNION ALL
      4             select 'Y', 2006, 'FEB' FROM DUAL UNION ALL
      5             select 'Y', 2006, 'MAR' FROM DUAL)
      6  -- END OF TEST DATA
      7  SELECT EMPID, YEAR, NVL(MAX(JAN),'N') AS JAN, NVL(MAX(FEB),'N') AS FEB, NVL(MAX(MAR),'N') AS MAR, NVL(MAX(APR),'N') AS APR
      8  FROM
      9  (
    10    SELECT EMPID, YEAR, 'Y' AS JAN, NULL AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'JAN' UNION ALL
    11    SELECT EMPID, YEAR, NULL AS JAN, 'Y' AS FEB, NULL AS MAR, NULL AS APR FROM t WHERE MONTH = 'FEB' UNION ALL
    12    SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, 'Y' AS MAR, NULL AS APR FROM t WHERE MONTH = 'MAR' UNION ALL
    13    SELECT EMPID, YEAR, NULL AS JAN, NULL AS FEB, NULL AS MAR, 'Y' AS APR FROM t WHERE MONTH = 'APR'
    14  )
    15  GROUP BY EMPID, YEAR
    16* ORDER BY 1,2
    SQL> /
    E       YEAR J F M A
    X       2006 Y N Y N
    Y       2006 Y Y Y N
    SQL>

  • Decode and Case

    Hi,
    What is the difference between decode and case?
    What are the cases in which we can use Decode and case
    Thanx

    you can not put Search CASE statements by using DECODE
    Eg:
    SELECT AVG(CASE WHEN e.salary > 2000 THEN e.salary ELSE 2000 END) "Average Salary" FROM employees e;Can't we?
    select avg(decode(sign(e.salary - 2000), 1, e.salary, 2000)) "Average Salary" from employees e;

  • ANSI Standard decode to CASE

    Hi all,
    select
    id
    ,one
    ,two
    From
         (select id
         , one
         , two
         , Max(decode(TYPE, "ER", 3*4, 4*7)
         from
              (select id
              , sum(one) as one
              , sum(two) as two
              from t1
              group by id)
         group by id, one, two
         ) temp inner join t2....
    The above query is just a sample structure to tell the problem that i am facing. It works fine, but i need to convert it to ANSI Standard.
    Hence I need to convert the DECODE to CASE statement. but if i convert to CASE, i need to use any of the GROUP BY FUNCTIONS, but if i use the MAX of any group by functions, the result would be wrong...how to overcome it
    Thanks

    Hi,
    Yes I did a silly mistake, the code I gave was for only the decode but when I converted it to case statement, there was a silly mistake, which i corrected.
    Now i corrected it to
    Select
         COL1
         , Col2
         , MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
         , Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
    From
         Group by....
    but now when i add a sum of the calc1 & calc2, I am not getting any results the column is blank. Is there anything that i missed
    Select
         COL1
         , Col2
         , MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END) AS CALC1
         , Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END) AS CALC2
         , (MAX(CASE WHEN Upper(TYPE) = 'YR' THEN Round(3.43*3, 2) END
         + Max(CASE WHEN Upper(TYPE) = 'ZP' THEN Round(3.12*12, 2) END)) as SUM3
    From
         Group by....
    Thanks

  • Converting Decode to Case

    I'm a beginner with Oracle SQL and I have a select statement with the following code:
    max(decode(EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD, '1000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '2000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE, '4000', EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE))
    I've been asked to create several more columns in a similar manner. My concern is that since this relies on 'decode' which is less efficient than 'case', that adding more columns using this approach will bog down an already not efficient query. Bottom line my problem is that I don't really understand this 'decode' since all the explanations of 'decode' I've found stop at four parameters.
    Could someone please show me how the expression above looks in If-then-else terms as well as comment on how to convert this 'decode' to 'case'? Thanks in advance!

    1) The if-then-else stmt
    -- Longest way
    if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' then
    return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
    elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' then
    return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
    elsif EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
    return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
    else return null;
    end if;
    -- shorter way
    if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '1000' or
    EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '2000' or
    EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD = '4000' then
    return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
    else return null;
    end if;
    -- shortest if-then-else code
    if EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in ('1000', '2000', '4000') then
    return value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE;
    else return null;
    end if;
    2) The MAX() function
    The MAX function will return the greatest value of EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE in step 1 above.
    2) Using CASE
    select
    max(case when (EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_CD in('1000','2000','4000'))
    then EDW.V_RECRUIT_TEST_RESULT_HIST.TEST_RESULT_TEST_SCORE end)
    into ...your_item_or_local_variable
    from EDW.V_RECRUIT_TEST_RESULT_HIST;
    Try the code above, good luck
    v/r
    Vien Tran

  • What is better and fast to use between decode and case

    Hi friends,
    i wanted to know what is better to use decode or case in sql,which gives faster result.
    thks
    sonal....

    Here's a very simple timing comparison. This table (actually it's a partition) has a little over 1 million rows.
    As you can see, the timing difference is trivial. (I ran an earlier query to fetch the blocks from disk).
    SQL> select sum(decode(balloon_flag
      2                   ,'Y',1
      3                   ,0
      4                   )
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.65
    SQL> select sum(case balloon_flag
      2                  when 'Y' then 1
      3                  else  0
      4             end
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.68
    SQL> select sum(case
      2                  when balloon_flag = 'Y' then 1
      3                  else  0
      4             end
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.46

  • How does decode and case works?

    Hi,
    I want to know how decode and case works? How they process the arguements. Please explain me with some examples.
    Thanks,
    Sarav
    Edited by: 943941 on Jul 3, 2012 1:42 AM

    welcome
    check this link
    https://forums.oracle.com/forums/ann.jspa?annID=1535
    you will find everything you need
    DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.
    This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non domestic'.
    SELECT product_id,
           DECODE (warehouse_id, 1, 'Southlake',
                                 2, 'San Francisco',
                                 3, 'New Jersey',
                                 4, 'Seattle',
                                    'Non domestic')
           "Location of inventory" FROM inventories
           WHERE product_id < 1775;Edited by: user 007 on Jul 3, 2012 2:40 PM

  • DECODE OR CASE - Which is better and why

    Oracle version: 11.2
    Problem: We have a huge table with 10M records, which needs to be processed daily.
    While loading data in table we have to handle condition if flag is =1 then '111' else '000'
    To implement this which one is efficient solution? .. using CASE or DECODE and why?
    Example:
    SQL> select flag,case when flag='Y' then '111' else '000' end from (select 'Y' as flag from dual union all select 'N' from dual);
    F CAS
    Y 111
    N 000
    SQL> select flag,decode(flag,'Y','111','000') from (select 'Y' as flag from dual union all select 'N' from dual);
    F DEC
    Y 111
    N 000

    Hi,
    For this job, they're equally efficient.
    For any job, where DECODE doesn't require a lot more code than CASE, they will be equally efficient.
    I recommend (almost) always using CASE. It's easier to understand and debug, and, if written correctly, will never be slower than DECODE.
    The only situation where I use DECODE is for very simple tasks (like the one you posted) where this is used inside a very complicated expression, and the slightly less coding needed for DECODE makes the larger statement easier to read.

  • DECODE OR CASE

    I have several codes that mean a specific thing that I need to measure. For instance HS002 and HS004 = MedNec, KS004, KS005, KS006, KS007,KS008= AdminDen. These codes are housed in one field the Denied Reason Field and It might be better to do a Case Statement to just change the code to fit in the one group and then I can count each group. Any help is appreciated. Thanks!!
    Im doing a report in which I need to get a count ot How Many Denied, How Many Approved with Denial Reasons and type. Can I get some direction on what would be the best way to do this. Here is my code so far:
    SELECT
    A1.AUTH_NUMBER,
    A1.AUTH_TYPE,
    A1..PLACE_OF_SERVICE,
    A1.OVERALL_STATUS,
    A1.CLOSED_REASON,
    A1.DENIED_REASON,
    DECODE (A1.DENIED_REASON,
    'HS002', 'MED NECESS',
    'HS004', 'MED NECESS')AS MEDNEC,
    A1.admit_primary_date,
    AP.decision_date,
    AP.ADVISOR_DECISION,
    PM.LAST_NAME,
    PM.PROVIDER_ID,
    A1.INSERT_DATETIME,
    A1.ACTIVE_PHYSICIAN_ADVISOR,
    MV.LINE_OF_BUSINESS
    FROM Auth_master a1
    INNER JOIN Auth_phys_advisor ap
    ON a1.auth_number=ap.auth_number
    INNER JOIN Prov_master pm
    ON ap.seq_prov_id=pm.seq_prov_id
    LEFT JOIN Note_master nm
    ON nm.seq_memb_id=a1.seq_memb_id
    INNER JOIN Member_mv mv
    ON mv.seq_memb_id=a1.seq_memb_id
    Where mv.Line_of_Business <>'SFS'
    AND A1.PLACE_OF_SERVICE IN ('11','21','22','24')
    AND a1.insert_datetime Between To_Date ('04/01/2012', 'MM/DD/YYYY') and To_Date ('04/30/2012','MM/DD/YYYY')

    select mydata.mednec, count(*)
    from (
    SELECT A1.AUTH_NUMBER,
           A1.AUTH_TYPE,
           A1.PLACE_OF_SERVICE,
           A1.OVERALL_STATUS,
           A1.CLOSED_REASON,
           A1.DENIED_REASON,
           Decode (A1.DENIED_REASON, 'HS002', 'MED NECESS',
                                     'HS004', 'MED NECESS',
                                     'KS008', 'ADMIN DEN') AS MEDNEC,
           A1.admit_primary_date,
           AP.decision_date,
           AP.ADVISOR_DECISION,
           PM.LAST_NAME,
           PM.PROVIDER_ID,
           A1.INSERT_DATETIME,
           A1.ACTIVE_PHYSICIAN_ADVISOR,
           MV.LINE_OF_BUSINESS
    FROM   Auth_master a1
           inner join Auth_phys_advisor ap
             ON a1.auth_number = ap.auth_number
           inner join Prov_master pm
             ON ap.seq_prov_id = pm.seq_prov_id
           left join Note_master nm
             ON nm.seq_memb_id = a1.seq_memb_id
           inner join Member_mv mv
             ON mv.seq_memb_id = a1.seq_memb_id
    WHERE  mv.Line_of_Business != 'SFS'
           AND A1.PLACE_OF_SERVICE IN ( '11', '21', '22', '24' )
           AND a1.insert_datetime BETWEEN To_date ('04/01/2012', 'MM/DD/YYYY') AND
                                          To_date (
                                          '04/30/2012', 'MM/DD/YYYY')
    ) mydata
    group by mydata.mednecEdited by: AlanWms on May 10, 2012 12:23 PM Added KS008/Admin den
    Edited by: AlanWms on May 10, 2012 12:25 PM

  • Decode and case statement in the update..

    Its is more to it, but I want to check with you guys, the experts on this, this look busy to me, it should be a more simplify way of doing it, do you think will work
    The government decide to change the ethnic codes, and we have to come with certain rules to in our report, anyway, do you think this will work? again It is more to it I declare my variables, this is just one part of the precedure.
    BEGIN
          UTL_FILE.fclose_all;
          v_file_handle := UTL_FILE.fopen (v_out_path, v_out_file, 'a');
          UTL_FILE.put_line (v_file_handle,
                             CHR (10) || TO_CHAR (SYSDATE, 'DD-MON-YYYY HH:MI:SS')
          UTL_FILE.put_line (v_file_handle, 'Entering  spbpers_update');
          SELECT upd_spbpers_upd_cur IS
              spriden_pidm,
              szscapp_birth_state,
              szscapp_birth_city,
              DECODE(szscapp_hisp_or_latino_ind,Y,'2',N,'1'),
              DECODE(szscapp_hisp_or_latino_options,XCM,'2',CUB,'2',MEX,'2',PRI,'2',XSM,'2',ESP,'2',XOH,'2'),  
              DECODE(szscapp_amer_indn_alaska_opt,XAN,'1','1',XCW,'1',XCH,'1',XCK,'1',XNV,'1',XSX,'1'),         
              DECODE(szscapp_amer_indn_alaska_other,XON,'1') (,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),  
              DECODE(szscapp_asian_options,IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),   ,          
              DECODE(szscapp_other_east_asia,(IND,'1',JPN,'1',KOR,'1',PAK,'1',PHL,'1',VNM,'1',XEA,'1',XIS,'1',XSA,'1'),            
              DECODE(szscapp_other_indian_subcont,XIS,'1'),    
              DECODE(szscapp_other_southeast_asia,XSA,'1'),   
              DECODE(szscapp_blk_or_afr_amer_opt,XAA,'1',XAF,'1',XCB,'1',XOA,'1'),     
              DECODE(szscapp_blk_or_afr_amer_other,XOA,'1'),   
              DECODE(szscapp_natve_hawaian_options,GUM,'1',XHI,'1',ASM,'1',XOP,'1'), 
              DECODE(szscapp_hawaiian_other,XOP,'1'),             
              DECODE(szscapp_white_options,XEU,'1',XME,'1',XOW,'1'),           
              DECODE(szscapp_white_other(XOW,'1')
         FROM
             saturn_midd.szscapp 
         WHERE
         spriden_id =  szscapp_id
         AND  spriden_ntyp_code = 'CAPL'
       IF upd_spbpers_upd_cur%ISOPEN
          THEN
             CLOSE upd_spbpers_upd_cur;
          END IF;
          OPEN upd_spbpers_upd_cur;
          LOOP
             FETCH upd_spbpers_upd_cur
              INTO v_pidm,v_birth_state,v_birth_city,v_latino_ind,v_latino_options,
                   v_indn_alaska_opt,v_indn_alaska_other,v_asian_options,
                   v_other_east_asia,v_other_indian_subcont,v_other_southeast_asia,
                   v_blk_or_afr_amer_opt,v_blk_or_afr_amer_other,v_natve_hawaian_options,           
                   v_hawaiian_other,v_white_options,v_white_other;
             EXIT WHEN upd_spbpers_upd_cur%NOTFOUND; 
             IF upd_spbpers_upd_cur%FOUND
               UPDATE  saturn.spbpers
                           set SPBPERS_ETHN_CODE  = CASE
                   WHEN v_latino_ind            IS NOT NULL THEN (spbpers_ethn_code = v_latino_ind,spbpers_activity_date = sysdate)     
                   WHEN v_latino_options        IS NOT NULL THEN (spbpers_ethn_code = v_latino_options,spbpers_activity_date = sysdate)
                   WHEN v_indn_alaska_opt       IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_opt,spbpers_activity_date = sysdate)
                   WHEN v_indn_alaska_other     IS NOT NULL THEN (spbpers_ethn_code = v_indn_alaska_other,spbpers_activity_date = sysdate)
                   WHEN v_asian_options         IS NOT NULL THEN (spbpers_ethn_code = v_asian_options,spbpers_activity_date = sysdate)
                   WHEN v_other_east_asia       IS NOT NULL THEN (spbpers_ethn_code = v_other_east_asia,spbpers_activity_date = sysdate)             
                   WHEN v_other_indian_subcont  IS NOT NULL THEN (spbpers_ethn_code = v_other_indian_subcont,spbpers_activity_date = sysdate)
                   WHEN v_other_southeast_asia  IS NOT NULL THEN (spbpers_ethn_code = v_other_southeast_asia,spbpers_activity_date = sysdate)
                   WHEN v_blk_or_afr_amer_opt   IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_opt,spbpers_activity_date = sysdate)
                   WHEN v_blk_or_afr_amer_other IS NOT NULL THEN (spbpers_ethn_code = v_blk_or_afr_amer_other,spbpers_activity_date = sysdate)
                   WHEN v_natve_hawaian_options IS NOT NULL THEN (spbpers_ethn_code = v_natve_hawaian_options,spbpers_activity_date = sysdate)
                   WHEN v_hawaiian_other        IS NOT NULL THEN (spbpers_ethn_code = v_hawaiian_other,spbpers_activity_date = sysdate)
                   WHEN v_white_options         IS NOT NULL THEN (spbpers_ethn_code = v_white_options,spbpers_activity_date = sysdate)
                   WHEN v_white_other           IS NOT NULL THEN (spbpers_ethn_code = v_white_other,spbpers_activity_date = sysdate)
                   WHEN v_birth_state           IS NOT NULL THEN (spbpers_stat_code_birth = v_birth_state,spbpers_activity_date = sysdate)
                   WHEN v_birth_city            IS NOT NULL THEN (spbpers_city_birth = v_birth_city,spbpers_activity_date = sysdate)
                   WHERE spbpers_pidm = v_pidm;
                  END
                     END IF;
          END LOOP;

    Did the procedure compile ?
    Doesn't look like a right Decode syntax.
    DECODE (col1,'VAL1','This','VAL2','That','ElseThis')
    means
    --Psuedocode
    IF col1 = 'VAL1' THEN 'This'
    IF col1 = 'VAL2' THEN 'That'
    ELSE 'ElseThis'You can use CASE statement Instead of DECODE
    CASE
    when      szscapp_amer_indn_alaska_other
         in ('XON','IND','JPN','KOR','PAK' ..... )  THEN '1'
    when      szscapp_hisp_or_latino_options
         in ('XCM','CUB','MEX','PRI','XSM','ESP','XOH' ...) THEN '2'
    END  SS

  • Query to find out sum by using decode or case

    Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
    INSNAME INSID STATUS
    AAA 1000 Scheduled
    AAA 1000 Processing
    BBB 1001 Inspector
    BBB 1001 Scheduled
    BBB 1001 Processing
    BBB 1001 Inspector
    CCC 1002 Scheduled
    CCC 1002 Processing
    CCC 1002 Inspector
    CCC 1002 Scheduled
    CCC 1002 Processing
    CCC 1002 Inspector
    Required Output...
    INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
    AAA 1000 1 1 0
    BBB 1001 1 1 2
    CCC 1002 2 2 2

    And if you want it with CASE statement:
    WITH test_table AS
       SELECT 'AAA' insname, 1000 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'AAA' insname, 1000 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL
    SELECT insname
          ,insid
          ,SUM(CASE WHEN status = 'Scheduled'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_scheduled
          ,SUM(CASE WHEN status = 'Processing'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_processing
          ,SUM(CASE WHEN status = 'Inspector'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_inspector         
      FROM test_table
    GROUP BY insname
            ,insid;
    Regards
    Arun

  • Decode or Case when statement

    Hi,
    I had a condition like
    test -table
    code - column
    t --> True
    f --> False
    I had two choices of writing query
    select decode(code,'t','True','False') from test;
    or
    select case code
    when 't' then 'True'
    else 'False'
    end
    from test;
    I want to know which one is best performance wise

    Wrong question really.. unless you writing a very tight loop doing 100's of 1000's of iterations using that type of conditional structure. And even then performance difference will not be that significant.
    The right question to ask is which one is more flexible and allows for the programmer that comes after you to read and understand and maintain your code. The CASE statement is in this regard, a lot better than a DECODE statement.
    Looking just at performance (which I suggest you do not do in isolation!!), I get mixed results using 10G Enterprise on different platforms (HP-UX vs SUN AMD) and operating systems (HP-UX vs Linux).
    On the former the DECODE is slightly faster. On the latter the CASE is slightly faster.
    Using the construct you've specified and doing a tight loop of a 100,000 iterations, the elapsed execution times are:
    HP-UX DECODE = 00:00:11.83
    HP-UX CASE = 00:00:12.32
    Linux/AMD DECODE =00:00:02.02
    Linux/AMD CASE = 00:00:01.84
    Obviosuly the CPU architecture plays a major role here. The AMD is considered as the best 64 CPU on the market. The HP-UX PARISC CPU (also 64bit), does not really compare raw performance wise. In addition this is a RISC CPU whereas the AMD CPU is I believe more CISC than RISC.
    Also interesting that the faster Sun AMD/Linux server I used for this benchmark is about 10% the price of the HP-UX server.. and about 5x faster ito raw speed as this benchmark showed. :-)
    An interesting exercise, but one with little real world value. Yes performance is important. But within PL/SQL, not to the level about debating whether a CASE or DECODE is faster. As I've mentioned, I believe the right question being one about readability and maintenance and not performance in this case.

Maybe you are looking for

  • Setting up a netgear n300 as wireless bridge/access point with airport extremee

    I just replaced my netgear wireless router with the airport to allow our many devices on the network without knocking each other off and also increase the speed.  But would like to use the netgear as a wireless bridge or access point so that I can st

  • Can I get new Mac Mini to boot without keyboard?

    I have a new Mac Mini, but no keyboard. Is there some way to get my MBP to emulate a bluetooth keyboard, or my iPhone? (I guess this would be a jailbreak app I just need to get past the first boot up stage to turn on screen sharing B

  • How to get smile sad face ?

    My Project If you push the �Smile� button you get a smiling face. If you push the �Sad� button you get a sad face. i am not able to get how to build smile face and sad face in the frame null

  • Precalculated web report

    Can I use a hierarchy filter in precalculated report? Also what kind of navigational features can I use in precalculted report? Thanks in advance

  • TCode for abap program performance testing

    Hi I have tuned a abap program which was consuming lot of time. I still have the old version with diff name. I would like to know the tcodes where in i could see the performance of the program. regards Balaji