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

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 Join with Nested Table

    Does anyone know how to (or whether you actually can) use ansi standard table joins with nested tables.
    Non-ansi standard would look something like this
    SELECT e.empno
    FROM departments d, TABLE(d.employees) e
    WHERE d.deptno = 10;
    Where d.employees is a nested table.
    But if I try ansi-standard I like such:
    SELECT e.empno
    FROM departments d
    JOIN TABLE(d.employees) e
    WHERE d.deptno = 10;
    I get
    ORA-00905: missing keyword
    because I have nothing to join it on.
    Your help is very much appreciated

    Both replies worked fine.
    I think I will go with the NATURAL JOIN as it seems the cleanest option.
    Thanks Guru 2748

  • How do I set date to ANSI standard format (YYMMDD) with the english language?

    Having difficulty with dates displayed in FireFox
    Microsoft desktop settings in Regional and Language options:
    YY MM DD (ANSI standard date format and Canadian Government standard (although dd mm yy and mm dd yy) are in common use)).
    Firefox setting: cannot find YY MM DD option, so used English/United Kingdom (DD MM YY) format
    An option for English language with ANSI date would be awesome.
    Is there a way to have Firefox display YY MM DD format without loosing English language?

    See if this helps: iPod: Changing the display language

  • 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

  • ANSI Standard Query

    HI Anyone can help me to have the correct ANSI Standard Query for the following
    SELECT * FROM AFFILIATED_PERSON ap WHERE EXISTS (
    SELECT 'x' FROM VB_HOUSEHOLD_MEMBERSHIP VBHM, VE_HOUSEHOLD_ORG_AFFIL VEHOAF
    WHERE VEHOAF.family_household_id = vbhm.family_household_id
    AND vehoaf.primary_type= 'ASGNLOCCAR'
    AND vbhm.affiliated_person_id = ap.AFFILIATED_PERSON_ID )
    AND EXISTS (
    SELECT 'x' FROM VB_PERSON_ORG_ASSIGNMENT vbpoas, VB_PERSON_ORG_AFFILIATION vbpoaf
    WHERE vbpoas.person_org_affil_id = vbpoaf.person_org_affil_id
    AND vbpoaf.affiliated_person_id = ap.AFFILIATED_PERSON_ID)
    ORDER BY ap.AFFILIATED_PERSON_ID
    Thanks in advance
    nattu

    hey,
    a. to me it seeems o.k.
    b. trust no one :)
    c. you can use: (from 10g sql reference)
    FLAGGER
    Syntax:
    FLAGGER = { ENTRY | INTERMEDIATE | FULL | OFF }
    The FLAGGER parameter specifies FIPS flagging, which causes an error message to be
    generated when a SQL statement issued is an extension of ANSI SQL92. FLAGGER is a
    session parameter only, not an initialization parameter.
    In Oracle Database, there is currently no difference between entry, intermediate, or full
    level flagging. After flagging is set in a session, a subsequent ALTER SESSION SET
    FLAGGER statement will work, but generates the message, ORA-00097. This allows
    FIPS flagging to be altered without disconnecting the session. OFF turns off flagging.
    e.g.
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE 10.2.0.1.0 Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    SQL> alter session set flagger=full;
    Session altered.
    SQL> select * from dual where dummy ='X';
    select * from dual where dummy ='X'
    ERROR at line 1:
    ORA-00097: use of Oracle SQL feature not in SQL92 Full Level
    ORA-06550: line 2, column 32:
    PLS-01454: No operator may be used with values of data type CHAR
    SQL> alter session set flagger=off;
    ERROR:
    ORA-00097: use of Oracle SQL feature not in SQL92 Full Level
    Session altered.
    SQL> select * from dual where dummy ='X';
    D
    X
    SQL>
    Amiel Davis
    Message was edited by:
    Amiel D.

  • Oracle - ANSI Standard

    I would like to know which ANSI standard does ORACLE follow?

    quoting wikipedia:
    SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standards (ISO) in 1987. Since then, the standard has been enhanced several times with added features. However, issues of SQL code portability between major RDBMS products still exist due to lack of full compliance with, or different interpretations of, the standard. Among the reasons mentioned are the large size and incomplete specification of the standard, as well as vendor lock-in.
    I think you may be better off trying to write whatever it is you want to write and seeing if it works that way across databases.
    the thing is (and this goes for all databases): some things are more efficient written in the proprietary extensions to SQL than in ANSI SQL. Take recursive with clauses vs the connect by clause etc.
    edit:
    The other thing to be wary of is the potential for differing functionality depending on how you write your queries. for example even though the database supports ANSI joins, I encountered a bug whereby the result_cache functionality doesn't work when you use ANSI join syntax:
    result_cache on tables
    Edited by: WhiteHat on Jun 21, 2012 11:58 AM

  • Price valuation with standard price in case of external procurement

    Dear All,
    I have the following scenario with my client.
    All the materials will be valuated with price control S, whether procurement type is E or F. We don't have any problem if procurement type is E. But where as if procurement type is F and price control indicator is S, the system calculating the moving average price and as well as standard price is also updating for some materials. For some materials there are not moving average prices.
    1. Can any one tell me when SAP calculates both moving average price and standard price? is there any setting to be done.(procurement type F and price control S)
    2. there are differences between moving average price and standard price, ofcourse there may be a difference, but in some cases the difference is more and higher than the standard price, this may cause financial loss to the company. How can solve this.
    full marks will be awarded.
    thanks & regards,
    Ramesh Balivada

    Hi
    Moving average is updated for External purchases although price control is standard price in all cases where there are movements in materials i.e., when they are purchases...
    Check for the materials where there are no updation of moving average and externally procured materials, there might not be any purchases..
    There would be no losses as even though it is calculating mov avg price, the stock, consumption is calculated with std price only.
    Check both the cases where there are std prices and check for cases where there are both prices getting updated.
    Regards,
    Suraj

  • 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.

Maybe you are looking for

  • ITunes crashes on opening..absolutely will not open!!!!!!!

    I was using 7.2 since it was upgraded with no problems, then all of a sudden it won't open. I can't pin it down to anything really. I'm getting really ****** at this point. I've tried re-installing, trashing the plists, running disk warrior, verifyin

  • MP3 Rocket Files

    I get MP3 files through a program called MP3 Rocket Pro. When I download files, they get copied to my itunes library. Why can't I access the files from my second computer, if they're copied to itunes? On a rare occasion I can click on the music butto

  • Issue with HelveticaNeue text not being included when printing to Adobe PDF?

    I have a document (Word 2010 docx) with a section of HelveticaNeue text on the front page that when printing through Adobe PDF the text does not appear. Using Acrobat 9.4.6 Tried the same document with: Win2Pdf - Displays correctly Converted to .doc

  • File Validatoin

    Hi, I need some implementation methodology for this interface. PI will pick up files from an FTP and sends to SAP for processing via proxy. The requirement is that if SAP failed to process the xml file for some reason, PI would need to make sure on n

  • HR ABAP BAPI_EMPLCOMM_DELIMIT

    I am trying to delimit the communication data which have multiple credit card data with different begin & end dates* Function module              BAPI_EMPLCOMM_DELIMIT                                                                                Imp