Using CASE WHEN in PL/SQL package

I am trying to convert the values in a selected column into 1 and 0 so that I can display all 1s in one column, all 0s in another. I am doing this in a PL/SQL package. However ORACLE compiler does not like the CASE construct.
Does anyone know how to group values in a column into several new columns. If CASE WHEN construct is not doable in PL/SQL, what alternatives are there? Thanks.
/******* My package starts here *******/
CREATE OR REPLACE PACKAGE TEST_NEED AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2);
END CVRR_MON_NEED;
CREATE OR REPLACE PACKAGE BODY TEST_NEED
AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2)
IS
TEST_START DATE := TO_DATE(STARTING_DATE,'MM/DD/YYYY');
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 > 40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND B.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0 END) QUALIFIED
FROM A, B, C, D
WHERE A.ID = B.B_ID
AND RTRIM(A.P_CODE) = C.P_CODE
AND A.P_ID = D.P_ID
AND A.P_ID < 99999999999999999999
AND A.E_DATETIME < SYSDATE
GROUP BY A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID;
v_RecordHolder v_Cursor%ROWTYPE;
BEGIN
OPEN v_Cursor;
FETCH v_CursorINTO v_RecordHolder ;
WHILE v_Cursor%FOUND LOOP
look for records in another table with matching keys of the cursor
if found then update by incrementing the existing values in the matching records with values of the current currsor row
else insert the current cursor row
FETCH v_Cursor INTO v_RecordHolder ;
END LOOP;
END procTEST_NEED;
END TEST_NEED;

I am trying to convert the values in a selected
column into 1 and 0 so that I can display all 1s in
one column, all 0s in another. I am doing this in a
PL/SQL package. However ORACLE compiler does not
like the CASE construct.
Does anyone know how to group values in a column into
several new columns. If CASE WHEN construct is not
doable in PL/SQL, what alternatives are there?
Thanks.
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START ,
, C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 ANDB.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0
END) QUALIFIEDUse the Decode function. This has been around in oracle SQL for ages and works like a case construct.
You would do something like
select ...
sum( decode (MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND
B.B_CODE IN ('11.1','222.2','272.4') 1,0 )

Similar Messages

  • How to use case when statements in ODI

    I need to put conditional logic before DVM look up in ODI. In the expression editor I put the following statement:-
    CASE WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS' THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=EBIZ_CELL.CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC' THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=EBIZ_CELL.CELL_DATA
    END
    It did not work,
    Under Operators ->All Executions, found the error:-
    905 : 42000 : java.sql.SQLException: ORA-00905: missing keyword
    The description in Session Task contained:-
    select     
         C1_JOURNAL_TEMPL
    from     APPS.POC_JOURNAL_TEMP_SOURCE_TBL POC_JOURNAL_TEMP_SOURCE_TBL, APPS.C$_0POC_JOURNAL_TEMP_TARGET_TB
    where     
         (1=1)
    And (CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME=C2_CELL_DATA
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)=C2_CELL_DATA
    END)
    Checked the above code in PL/SQL Developer but it gave errors.
    In PL/SQL developer tried to check a simple query using case-when but even that is giving errors in the case-when portion.
    The query is as follows:-
    select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    where
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'
    then
    1
    when 'RAC'
    then
    2
    when 'XXX'
    then
    3
    else
    end
    I would like to know what is wrong with the above code.
    Please let me know what is the correct way of using case-when in PL/SQL as well as in ODI.

    Your ODI case statement and PL/SQL Case statement both looks confusing.
    You are writing case statement under where clause which is not a good practise. If you want to implement logic like-
    select a,b,c from <table>
    where
    when cond^n^ 1 then do 1
    when cond^n^ 2 then do 2
    then better you seperate your query for each filter and do a union, in other words-
    select a,b,c from <table>
    where cond^n^ 1
    union
    select a,b,c from <table>
    where cond^n^ 2
    If you are writing case staement to retrieve a value/column (EBIZ_CELL.CELL_DATA) then no need to include it under filter.
    ODI case for column EBIZ_CELL.CELL_DATA will be:
    CASE
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='RCS'
    THEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    WHEN POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME='FDC'
    THEN CONCAT(POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME,POC_JOURNAL_TEMP_SOURCE_TBL.BOOK_CODE)
    END
    Pl/SQL query-
    select phase_code, accounting_period,
    case POC_JOURNAL_TEMP_SOURCE_TBL.APPLICATION_NAME
    when 'EXT'then 1
    when 'RAC' then 2
    when 'XXX' then 3
    else 'default value' --- (if no else needed then can also remove else part)
    end,
    sum(eff_cc) as BD_Eff_QTD
    from prj_detail
    Suggested as per what is understood, hope it helps.
    Edited by: 939451 on Jul 5, 2012 12:47 AM
    Edited by: 939451 on Jul 5, 2012 12:48 AM

  • Using case when statement or decode stament in where clause

    hi gems..
    i have a problem in the following query..
    i am trying to use case when statement in the where clause of a select query.
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
    cr.salary as salary
    from customer_details cr
    where (case when '>' = '>' then 'cr.salary > 5000'
    when '>' = '<' then 'cr.salary < 5000'
    when '>' = '=' then 'cr.salary = 5000'
    else null
    end);
    the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
    thats why for running the query, i have put '>' in that place.
    so the original query will look like this(for your reference):
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
    cr.salary as salary
    from customer_details cr
    where (case when variable = '>' then 'cr.salary > 5000'
    when variable = '<' then 'cr.salary < 5000'
    when variable = '=' then 'cr.salary = 5000'
    else null
    end);
    so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
    if the user selects '<' then the filter will be "where cr.salary < 5000"
    if the user selects '=' then the filter will be "where cr.salary = 5000"
    but i am getting the error "ORA 00920:invalid relational operator"
    please help..thanks in advance..

    Hi,
    select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
           cr.salary                                      as salary
    from customer_details cr
    where (    v_variable = 'bigger'
           and cr.salary > 5000
       or (    v_variable = 'less'
          and cr.salary < 5000
       or (    v_variable = 'eq'
            and cr.salary = 5000
           )Edited by: user6806750 on 22.12.2011 14:56
    For some reason I can't write in sql '<', '>', '='

  • Using Case When

    I have a situation where i need to write a case when sql expression to fetch certain rows from a table.
    How to use CASE WHEN THEN ELSE END in a report query ?
    Explanation with a simple example will be of great help.
    thanks

    The javadoc for oracle.toplink.expressions.Expression
    contains some information.
    The following is an example of how to build the case statement.
    This will use case to replace the keys in the caseTable with the values. The default value (the ELSE) is provided as the second argument.
         Hashtable caseTable = new Hashtable(3);
         caseTable.put("Bob", "Bobby");
         caseTable.put("Susan", "Susie");
         caseTable.put("Eldrick", "Tiger");
         Expression expression = builder.get("firstName").caseStatement(caseTable, "No-Nickname").equal("Bobby");

  • How to use case when function to calculate time ?

    Dear All,
    May i know how to use case when function to calculate the time ?
    for the example , if the First_EP_scan_time is 12.30,  then must minus 30 min.  
    CASE WHEN FIRSTSCAN.EP_SHIFT <> 'R1' AND FIRSTSCAN.EP_SHIFT <> 'R2'
    THEN ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF((CASE WHEN SHIFTCAL.EP_SHIFT = 'N1'
    THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(FIRSTSCAN.EP_SCAN_DATE ,8)),112) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
    ELSE LEFT(FIRSTSCAN.EP_SCAN_DATE ,8) + ' ' + REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END),12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0 - 0.25) AS FLOAT),2)
    ELSE ROUND(CAST((DATEDIFF(MINUTE,CAST(STUFF(STUFF(FIRSTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME),CAST(STUFF(STUFF(LASTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) / 60.0) AS FLOAT),2) END AS OTWORK_HOUR

    Do not use computations in a declarative language.  This is SQL and not COBOL.
    Use a table of time slots set to one more decimal second of precision than your data. You can now use temporal math to add it to a DATE to TIME(1) get a full DATETIME2(0). Here is the basic skeleton. 
    CREATE TABLE Timeslots
    (slot_start_time TIME(1) NOT NULL PRIMARY KEY,
     slot_end_time TIME(1) NOT NULL,
     CHECK (start_time < end_time));
    INSERT INTO Timeslots  --15 min intervals 
    VALUES ('00:00:00.0', '00:14:59.9'),
    ('00:15:00.0', '00:29:59.9'),
    ('00:30:00.0', '00:44:59.9'),
    ('00:45:00.0', '01:00:59.9'), 
    ('23:45:00.0', '23:59:59.9'); 
    Here is the basic query for rounding down to a time slot. 
    SELECT CAST (@in_timestamp AS DATE), T.start_time
      FROM Timeslots AS T
     WHERE CAST (@in_timestamp AS TIME)
           BETWEEN T.slot_start_time 
               AND T.slot_end_time;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • About case when and the sql clause

    Hi:
    The following is my sql clause:
    SELECT t.*,b.name
    FROM dbtest1 t
    LEFT OUTER JOIN dbtest b ON t.NO = b.empno
    WHERE t.ChineseName like '%'||:ChineseName||'%' AND b.name like '%'||:name||'%'
    ORDER BY t.ChineseName
    The main problem is I hope to check the b.name if it is null it can be passed as NVL function, so I try using case when, but it not working.
    When b.name exist in where clause, the result columns will not include the data without b.name(or implies the b.name is NULL); And that
    make the result data not exactly right.
    is it possible to use case when to make the following snippet implemented:
    case when b.name is not NULL then b.name like '%'||:name||'%'
    else b.name = NULL(b.name=b.name seems not work to parsing null data)
    end
    Thanks a lot.

    962769 wrote:
    Hi:
    The following is my sql clause:
    SELECT t.*,b.name
    FROM dbtest1 t
    LEFT OUTER JOIN dbtest b ON t.NO = b.empno
    WHERE t.ChineseName like '%'||:ChineseName||'%' AND b.name like '%'||:name||'%'
    ORDER BY t.ChineseName
    The main problem is I hope to check the b.name if it is null it can be passed as NVL function, so I try using case when, but it not working.
    When b.name exist in where clause, the result columns will not include the data without b.name(or implies the b.name is NULL); And that
    make the result data not exactly right.
    is it possible to use case when to make the following snippet implemented:
    case when b.name is not NULL then b.name like '%'||:name||'%'
    else b.name = NULL(b.name=b.name seems not work to parsing null data)
    end
    Thanks a lot.Your question isn't very clear. But below is the statement as I make a meaning out of it.
    SELECT t.*,b.name
    FROM dbtest1 t
    LEFT OUTER JOIN dbtest b ON t.NO = b.empno
    WHERE t.ChineseName like '%'||:ChineseName||'%'
         AND b.name like '%'||NVL(:name, b.name)||'%'
    ORDER BY t.ChineseNameIf you want to use case, then:
    SELECT t.*,b.name
    FROM dbtest1 t
    LEFT OUTER JOIN dbtest b ON t.NO = b.empno
    WHERE t.ChineseName like '%'||:ChineseName||'%'
         AND b.name like '%'|| case when b.name is not null then :name else b.name end ||'%'
    ORDER BY t.ChineseNameIf this is not what you are looking for then, read {message:id=9360002} and post the relevant details with an example (Create Table script, Sample Data creation script and the Expected output of the sample data).

  • How to use case when in Select qry?

    Hi Friends,
    I want to use Case when in Select qry, my situation is like this
    SELECT bmatnr blgort bj_3asiz bmat_kdauf b~mat_kdpos
           SUM( ( case when bshkzg = 'S' then bmenge else 0 END ) -
            ( case when bshkzg = 'H' then bmenge else 0 END ) ) AS qty
           INTO corresponding fields of table it_projsal
        FROM mseg AS b
            INNER JOIN mkpf AS a ON bmblnr = amblnr
                                AND bmandt = amandt
        WHERE abudat  < '20061201' AND blgort IN ('1050')
          and b~mandt = '350'
        GROUP BY bmatnr bj_3asiz bmat_kdauf bmat_kdpos b~lgor
    If we give like this it gives an error.
    Please help me, how to use or handle in select qry itself.
    Regards
    Shankar

    this is not a way to select data from the DB tables.
    first get all the data from the DB tables then u have to do SUM Ups .
    Regards
    prabhu

  • Update multiple rows using CASE WHEN

    I have the table ACCOUNT of structure as follow: 
    ACCOUNT_ID
    ACCOUNT_STATUS
    004460721
    2
    042056291
    5
    601272065
    3
    I need to update the three rows at once using one SELECT statement such that, the second column will be 5, 3, 2 respectively.
    I used the following query but seems there is something missing
    UPDATE ACCOUNT
    SET ACCOUNT_STATUS = CASE  
    WHEN ACCOUNT_STATUS = '004460721' THEN 5 
    WHEN ACCOUNT_STATUS = '042056291' THEN 3 
    WHEN ACCOUNT_STATUS = '601272065' THEN 2 
    WHERE ACCOUNT_ID IN ('004460721','042056291','601272065') 
    My question, is this way correct? if no, can I use CASE WHEN statement and how or I only have choice of using SUB-SELECT to acheive that in one statement?

    Hi,
    Hawk333 wrote:
    I have the table ACCOUNT of structure as follow:
    ACCOUNT_ID
    ACCOUNT_STATUS
    004460721
    2
    042056291
    5
    601272065
    3
    I need to update the three rows at once using one SELECT statement such that, the second column will be 5, 3, 2 respectively.
    I used the following query but seems there is something missing
    UPDATE ACCOUNT
    SET ACCOUNT_STATUS = CASE  
    WHEN ACCOUNT_STATUS = '004460721' THEN 5 
    WHEN ACCOUNT_STATUS = '042056291' THEN 3 
    WHEN ACCOUNT_STATUS = '601272065' THEN 2 
    WHERE ACCOUNT_ID IN ('004460721','042056291','601272065') 
    My question, is this way correct? if no, can I use CASE WHEN statement and how or I only have choice of using SUB-SELECT to acheive that in one statement?
    What happens when you try it?
    Did you mean "WHEN ACCOUNT_ID = ..."?
    A CASE expressions always needs an END keyword.
    Depending on your requirements (that is, why are those rows being changed, and how do you determine the new values) a CASE expression in an UPDATE statement, similar to what you posted, could be a good way to do it.  MERGE (instead of UPDATE) would also be an option, especially if you want to avoid updating rows that already happen to have the correct values.

  • Insert using case when

    hello ,
    i wanna use "case when" statement in the insert , i implement it like this :
    currentyear:=1;
    insert into test1 (ATTRIB1,ATTRIB2) values ((case when currentyear=1 then 'ok' else 'notOK' end),'val2' );
    but it didn't work, i got this error :
    ORA-14551: cannot perform a DML operation inside a query
    Regards
    Elyes
    Edited by: user10393090 on 5 janv. 2009 03:32

    Better approach is ->
    insert into test1 (ATTRIB1,ATTRIB2)
    select case
             when &currentyear=1 then
              'ok'
           else
             'notOK'
           end AS attrib1,
           'val2' AS attrib2
    from dual;N.B.: Not Tested...
    Regards.
    Satyaki De.

  • Is there any way to simplyfy this sql using case when or any other condition

    Hi,
    Please ca you advise me to simplyfy the given sql         
     SELECT  
           CASE 
             WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN SIZE    
            END AS off_1st_size_txt_out,
    CASE 
             WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN make    
            END AS off_1st_make_txt_out,
    CASE 
             WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN pattern    
            END AS off_1st_pat_txt_out,
    CASE
     WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth1    
            END AS off_1st_size_out,
    CASE
    WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth2    
            END AS off_1st_make_out,
    CASE
    WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth3    
            END AS off_1st_pat_out,
    --1st near side outer
    CASE 
             WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN SIZE    
            END AS near_1st_size_txt_out,
    CASE 
             WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN make    
            END AS near_1st_make_txt_out,
    CASE 
             WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN pattern    
            END AS near_1st_pat_txt_out,
    CASE
     WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN TreadDepth1    
            END AS near_1st_size_out,
    CASE
    WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN TreadDepth2    
            END AS near_1st_make_out,
    CASE
    WHEN  AxleNo=1  and AxleType = 'NEARSIDE OUTER'
                THEN TreadDepth3    
            END AS near_1st_pat_out,
    --2nd Offside Outer
           CASE 
             WHEN  AxleNo=2  and AxleType = 'OFFSIDE OUTER'
                THEN SIZE    
            END AS off_2nd_size_txt_out,
    CASE 
             WHEN  AxleNo=2  and AxleType = 'OFFSIDE OUTER'
                THEN make    
            END AS off_2nd_make_txt_out,
    CASE 
             WHEN  AxleNo=2  and AxleType = 'OFFSIDE OUTER'
                THEN pattern    
            END AS off_2nd_pat_txt_out,
    CASE
     WHEN  AxleNo=2  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth1    
            END AS off_2nd_size_out,
    CASE
    WHEN  AxleNo=2  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth2    
            END AS off_2nd_make_out,
    CASE
    WHEN  AxleNo=1  and AxleType = 'OFFSIDE OUTER'
                THEN TreadDepth3    
            END AS off_2nd_pat_out
    from my fleets
    With Many Thanks
    Pol
    polachan

    Hello, 
    the same result ( maybe with records in slightly different order ), you can obtain using this code snippet.
    SELECT   SIZE AS off_1st_size_txt_out,
      make AS off_1st_make_txt_out,
    pattern AS off_1st_pat_txt_out,
    TreadDepth1 AS off_1st_size_out,
    TreadDepth2 AS off_1st_make_out,
    TreadDepth3 AS off_1st_pat_out,
    NULL AS near_1st_size_txt_out,
      NULL AS near_1st_make_txt_out,
    NULL AS near_1st_pat_txt_out,
    NULL AS near_1st_size_out,
    NULL AS near_1st_make_out,
    NULL AS near_1st_pat_out,
    NULL AS off_2nd_size_txt_out,
      NULL AS off_2nd_make_txt_out,
    NULL AS off_2nd_pat_txt_out,
    NULL AS off_2nd_size_out,
    NULL AS off_2nd_make_out,
    NULL AS off_2nd_pat_out
    WHERE AxleNo=1  and AxleType = 'OFFSIDE OUTER'
    from my fleets
    UNION
    SELECT   NULL AS off_1st_size_txt_out,
      NULL AS off_1st_make_txt_out,
    NULL AS off_1st_pat_txt_out,
    NULL AS off_1st_size_out,
    NULL AS off_1st_make_out,
    NULL AS off_1st_pat_out,
    SIZE AS near_1st_size_txt_out,
      make AS near_1st_make_txt_out,
    pattern AS near_1st_pat_txt_out,
    TreadDepth1 AS near_1st_size_out,
    TreadDepth2 AS near_1st_make_out,
    TreadDepth3 AS near_1st_pat_out,
    NULL AS off_2nd_size_txt_out,
      NULL AS off_2nd_make_txt_out,
    NULL AS off_2nd_pat_txt_out,
    NULL AS off_2nd_size_out,
    NULL AS off_2nd_make_out,
    NULL AS off_2nd_pat_out
    WHERE AxleNo=1  and AxleType = 'NEARSIDE OUTER'
    from my fleets
    UNION
    SELECT   NULL AS off_1st_size_txt_out,
      NULL AS off_1st_make_txt_out,
    NULL AS off_1st_pat_txt_out,
    NULL AS off_1st_size_out,
    NULL AS off_1st_make_out,
    NULL AS off_1st_pat_out,
    NULL AS near_1st_size_txt_out,
      NULL AS near_1st_make_txt_out,
    NULL AS near_1st_pat_txt_out,
    NULL AS near_1st_size_out,
    NULL AS near_1st_make_out,
    NULL AS near_1st_pat_out,
    SIZE AS off_2nd_size_txt_out,
      make AS off_2nd_make_txt_out,
    pattern AS off_2nd_pat_txt_out,
    TreadDepth1 AS off_2nd_size_out,
    TreadDepth2 AS off_2nd_make_out,
    TreadDepth3 AS off_2nd_pat_out
    WHERE AxleNo=2  and AxleType = 'OFFSIDE OUTER'
    from my fleets
    If you find it easier to understand and to maintain. The result is made using "UNION" .
    Šimon

  • Using Case when statement in catalog doesnt result in the correct sql

    I am running into an issue on my current project wherein, I am not able to see the correct formula in the physical query as I am seeing it in the logical query:
    Logical query:
    SET VARIABLE QUERY_SRC_CD='Report',SAW_SRC_PATH='/shared/Email/SFDC_InitRespTimel_Main';SELECT s_0, s_1, s_2, s_3, s_4, s_5 FROM (
    SELECT
    0 s_0,
    "CIN"."Calendar"."FIis Yr Nm" s_1,
    "CIN"."Calendar"."Fis Qtr Nm" s_2,
    "CIN"."SF Origin Dim"."Group" s_3,
    **CASE WHEN MIN("CIN"."SF_EailMsg_L"."Msg Dt") IS NOT NULL THEN SUM(TIMESTAMPDIFF(SQL_TSI_HOUR,MIN("SF_EailMsg_L"."Msg Dt"),"CIN"."SF_Case_L_Fact"."Created Dt"))/count("CIN"."SF_Case_L_Fact"."Case Number") END s_4,**
    **REPORT_AGGREGATE(CASE WHEN MIN("CIN"."SF_EailMsg_L"."Msg Dt") IS NOT NULL THEN SUM(TIMESTAMPDIFF(SQL_TSI_HOUR,MIN("SF_EailMsg_L"."Msg Dt"),"CIN"."SF_Case_L_Fact"."Created Dt"))/count("CIN"."SF_Case_L_Fact"."Case Number") END BY "CIN"."Calendar"."Fis Qtr Nm","CIN"."SF Origin Dim"."Group") s_5**
    FROM "CIN"
    WHERE
    (("Calendar"."FISCAL_YEAR_NAME" IN ('FY2011', 'FY2012', 'FY2013')) AND ("SF_EailMsg_L"."Status" = '3') AND ("Calendar"."FISCAL_QUARTER_NAME" <> 'Q1 FY2011'))
    ) djm ORDER BY 1, 2 ASC NULLS LAST, 3 ASC NULLS LAST, 4 ASC NULLS LAST
    Physical Query:
    select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3,
    D1.c4 as c4
    from
    (select T13596.CRTDDT_DT as c1,
    T4813.FISCAL_YEAR_NAME as c2,
    T4813.FISCAL_QUARTER_NAME as c3,
    T13313.GRP as c4,
    ROW_NUMBER() OVER (PARTITION BY T4813.FISCAL_QUARTER_NAME, T13313.GRP, T13596.CRTDDT_DT ORDER BY T4813.FISCAL_QUARTER_NAME ASC, T13313.GRP ASC, T13596.CRTDDT_DT ASC) as c5
    from
    DW_SF_EMLMSSG_L T13275,
    SF_ORGN_L_DIM T13313,
    CIN_CALENDARS_DIM T4813,
    DW_SF_CS_L T13596 /* SF_CS_L_Fact */
    where ( T4813.CALENDAR_KEY = T13596.CRDT_KEY and T13275.PRNTID = T13596.ID and T13275.STTS = '3' and T13313.ORGN_KEY = T13596.ORGN_KEY and (T4813.FISCAL_YEAR_NAME in ('FY2011', 'FY2012', 'FY2013')) and T4813.FISCAL_QUARTER_NAME <> 'Q1 FY2011' )
    ) D1
    where ( D1.c5 = 1 )
    WITH
    SAWITH0 AS (select min(T13275.MSSGDT_DT) as c1,
    count(T13596.CSNMBR) as c2,
    T4813.FISCAL_YEAR_NAME as c5,
    T4813.FISCAL_QUARTER_NAME as c6,
    T13313.GRP as c7
    from
    DW_SF_EMLMSSG_L T13275,
    SF_ORGN_L_DIM T13313,
    CIN_CALENDARS_DIM T4813,
    DW_SF_CS_L T13596 /* SF_CS_L_Fact */
    where ( T4813.CALENDAR_KEY = T13596.CRDT_KEY and T13275.PRNTID = T13596.ID and T13275.STTS = '3' and T13313.ORGN_KEY = T13596.ORGN_KEY and (T4813.FISCAL_YEAR_NAME in ('FY2011', 'FY2012', 'FY2013')) and T4813.FISCAL_QUARTER_NAME <> 'Q1 FY2011' )
    group by T4813.FISCAL_QUARTER_NAME, T4813.FISCAL_YEAR_NAME, T13313.GRP)
    select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3,
    D1.c4 as c4,
    D1.c5 as c5,
    D1.c6 as c6,
    D1.c7 as c7
    from
    (select D1.c1 as c1,
    D1.c2 as c2,
    min(D1.c1) over (partition by D1.c6, D1.c7) as c3,
    sum(D1.c2) over (partition by D1.c6, D1.c7) as c4,
    D1.c5 as c5,
    D1.c6 as c6,
    D1.c7 as c7,
    ROW_NUMBER() OVER (PARTITION BY D1.c6, D1.c7 ORDER BY D1.c6 ASC, D1.c7 ASC) as c8
    from
    SAWITH0 D1
    ) D1
    where ( D1.c8 = 1 )
    I want to know if you have seen this scenario earlier?
    Basically, the case statement in the logical sql is not getting executed correctly in the physical sql. Also,I see a SAWWITH0 in the query. Have you seen this before.
    If you have any inputs on how to fix this, please share.
    Thanks in advance.
    -Mayank Sharma

    Hello,
    This is the logical and physical query that I am getting now.
    My question is that, why am I not seeing the division operator (/) anywhere in the physical query. I am worried that the physical query is not being executed correctly.
    Please clarify:
    SET VARIABLE QUERY_SRC_CD='Report',SAW_DASHBOARD='/shared/Email/Email',SAW_DASHBOARD_PG='Initial Resp Time',SAW_SRC_PATH='/shared/Email/SFDC_InitRespTimel_Main';SELECT s_0, s_1, s_2, s_3, s_4, s_5 FROM (
    SELECT
    0 s_0,
    "CIN"."Calendar"."FIis Yr Nm" s_1,
    "CIN"."Calendar"."Fis Qtr Nm" s_2,
    "CIN"."SF Origin Dim"."Group" s_3,
    CASE WHEN MIN("CIN"."SF_EailMsg_L"."Msg Dt") IS NOT NULL THEN SUM(TIMESTAMPDIFF(SQL_TSI_HOUR,MIN("SF_EailMsg_L"."Msg Dt"),"CIN"."SF_Case_L_Fact"."Created Dt"))*1.0/count("CIN"."SF_Case_L_Fact"."Case Number")*1.0 END s_4,
    REPORT_AGGREGATE(CASE WHEN MIN("CIN"."SF_EailMsg_L"."Msg Dt") IS NOT NULL THEN SUM(TIMESTAMPDIFF(SQL_TSI_HOUR,MIN("SF_EailMsg_L"."Msg Dt"),"CIN"."SF_Case_L_Fact"."Created Dt"))*1.0/count("CIN"."SF_Case_L_Fact"."Case Number")*1.0 END BY "CIN"."Calendar"."Fis Qtr Nm","CIN"."SF Origin Dim"."Group") s_5
    FROM "CIN"
    WHERE
    (("Calendar"."FISCAL_YEAR_NAME" IN ('FY2011', 'FY2012', 'FY2013')) AND ("SF_EailMsg_L"."Status" = '3') AND ("Calendar"."FISCAL_QUARTER_NAME" <> 'Q1 FY2011'))
    ) djm ORDER BY 1, 2 ASC NULLS LAST, 3 ASC NULLS LAST, 4 ASC NULLS LAST
    [2012-10-25T22:20:58.000+00:00] [OracleBIServerComponent] [TRACE:2] [USER-23] [] [ecid: a5bccb33dce3bd12:-c49596b:13a898b42b5:-8000-0000000000007756] [tid: 601c] [requestid: 1d4c0001] [sessionid: 1d4c0000] [username: weblogic] -------------------- General Query Info: [[
    Repository: Star, Subject Area: CIN, Presentation: CIN
    [2012-10-25T22:20:58.000+00:00] [OracleBIServerComponent] [TRACE:2] [USER-18] [] [ecid: a5bccb33dce3bd12:-c49596b:13a898b42b5:-8000-0000000000007756] [tid: 601c] [requestid: 1d4c0001] [sessionid: 1d4c0000] [username: weblogic] -------------------- Sending query to database named CIN (id: <<177780>>), connection pool named CINADMIN: [[
    select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3,
    D1.c4 as c4
    from
    (select T13596.CRTDDT_DT as c1,
    T4813.FISCAL_YEAR_NAME as c2,
    T4813.FISCAL_QUARTER_NAME as c3,
    T13313.GRP as c4,
    ROW_NUMBER() OVER (PARTITION BY T4813.FISCAL_QUARTER_NAME, T13313.GRP, T13596.CRTDDT_DT ORDER BY T4813.FISCAL_QUARTER_NAME ASC, T13313.GRP ASC, T13596.CRTDDT_DT ASC) as c5
    from
    DW_SF_EMLMSSG_L T13275,
    SF_ORGN_L_DIM T13313,
    CIN_CALENDARS_DIM T4813,
    DW_SF_CS_L T13596 /* SF_CS_L_Fact */
    where ( T4813.CALENDAR_KEY = T13596.CRDT_KEY and T13275.PRNTID = T13596.ID and T13275.STTS = '3' and T13313.ORGN_KEY = T13596.ORGN_KEY and (T4813.FISCAL_YEAR_NAME in ('FY2011', 'FY2012', 'FY2013')) and T4813.FISCAL_QUARTER_NAME <> 'Q1 FY2011' )
    ) D1
    where ( D1.c5 = 1 )
    [2012-10-25T22:20:58.000+00:00] [OracleBIServerComponent] [TRACE:2] [USER-18] [] [ecid: a5bccb33dce3bd12:-c49596b:13a898b42b5:-8000-0000000000007756] [tid: 601c] [requestid: 1d4c0001] [sessionid: 1d4c0000] [username: weblogic] -------------------- Sending query to database named CIN (id: <<178213>>), connection pool named CINADMIN: [[
    select D1.c1 as c1,
    D1.c2 as c2,
    D1.c3 as c3,
    D1.c4 as c4,
    D1.c5 as c5,
    D1.c6 as c6,
    D1.c7 as c7
    from
    (select D1.c1 as c1,
    D1.c2 as c2,
    min(D1.c1) over (partition by D1.c6, D1.c7) as c3,
    sum(D1.c2) over (partition by D1.c6, D1.c7) as c4,
    D1.c5 as c5,
    D1.c6 as c6,
    D1.c7 as c7,
    ROW_NUMBER() OVER (PARTITION BY D1.c6, D1.c7 ORDER BY D1.c6 ASC, D1.c7 ASC) as c8
    from
    (select min(T13275.MSSGDT_DT) as c1,
    count(T13596.CSNMBR) as c2,
    T4813.FISCAL_YEAR_NAME as c5,
    T4813.FISCAL_QUARTER_NAME as c6,
    T13313.GRP as c7
    from
    DW_SF_EMLMSSG_L T13275,
    SF_ORGN_L_DIM T13313,
    CIN_CALENDARS_DIM T4813,
    DW_SF_CS_L T13596 /* SF_CS_L_Fact */
    where ( T4813.CALENDAR_KEY = T13596.CRDT_KEY and T13275.PRNTID = T13596.ID and T13275.STTS = '3' and T13313.ORGN_KEY = T13596.ORGN_KEY and (T4813.FISCAL_YEAR_NAME in ('FY2011', 'FY2012', 'FY2013')) and T4813.FISCAL_QUARTER_NAME <> 'Q1 FY2011' )
    group by T4813.FISCAL_QUARTER_NAME, T4813.FISCAL_YEAR_NAME, T13313.GRP
    ) D1
    ) D1
    where ( D1.c8 = 1 )

  • Connection Reset when compiling PL/SQL Package

    Recently a strange issue appeared on my office PC. When I try To compile a PL/SQL package on specific Oracle instance I get an error message:
    Error: Io exception: Connection reset by peer: socket write error
    And the connection is reset. I even cannot reconnect to database schema. To open connection again I have to restart SQL Developer. In spite of this issue I can execute SELECT queries in SQL worksheet and view data in tables. Error message appears only when compiling packages in any schema on database instance in our local network and only on my PC. Other office PCs works fine without any errors. I am able to compile packages on remote database from my PC.
    Same error message shows up in different SQL Developer versions and also in JDeveloper. SQL Developer restart, Windows restart, database instance restart doesn't help.
    Used software:
    SQL Developer versions: 1.2.1 and 1.1
    JDeveloper version: 10.1.3.2
    Oracle Database on local network: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit
    Remote Oracle Database: Oracle Database 10g Release 10.2.0.3.0 - Production
    OS: Windows XP Pro SP2
    Thanks,
    Raymond

    I am trying to convert the values in a selected
    column into 1 and 0 so that I can display all 1s in
    one column, all 0s in another. I am doing this in a
    PL/SQL package. However ORACLE compiler does not
    like the CASE construct.
    Does anyone know how to group values in a column into
    several new columns. If CASE WHEN construct is not
    doable in PL/SQL, what alternatives are there?
    Thanks.
    CURSOR v_Cursor IS
    SELECT A.D_CODE, A.M_CODE, TEST_START ,
    , C.C_NAME,C.P_ID,
    SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
    40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 ANDB.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0
    END) QUALIFIEDUse the Decode function. This has been around in oracle SQL for ages and works like a case construct.
    You would do something like
    select ...
    sum( decode (MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
    40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND
    B.B_CODE IN ('11.1','222.2','272.4') 1,0 )

  • Using case when statement in the select query to create physical table

    Hello,
    I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
    I have a physical table based on a select table with one column.
    SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
    I also have a customer table.
    In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
    In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
    EXECUTION
    When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
    everything works as expected. YE!!
    Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
    SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
    FROM DUAL
    Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
    surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
    SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
    If anyone has any explanation to this error and how we can achieve the same, please help.
    Thanks.

    Hello,
    Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
    I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
    Here is what I am trying to do. the select query of the physical table is as follows.
    SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
    The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
    Thanks.

  • Using case when to an aggregate function

    Hi,
    I have a sql statement like below,
    Select CASE WHEN (Sum(Amount) Over (Partition By Name),1,1) = '-' THEN 0 ELSE Sum(Amount) Over (Partition By Name) END AS Amount_Person
    From tbPerson
    But when I run the sql statement above I got error ORA-00920: invalid relational operator. What I'm trying to do is when the total amount for each person is negative then it will return 0 else it will return the positive value. I dont want to use the GROUP BY function. Is there any other way than using the Sum Over function? Thanks

    Like this?
    SELECT CASE WHEN Sum(Amount) Over (Partition By Name) < 0 THEN 0
                ELSE Sum(Amount) Over (Partition By Name)
           END AS Amount_Person
    FROM tbPerson
    ;or using GREATEST function :
    SELECT GREATEST(
             Sum(Amount) Over (Partition By Name)
           , 0
           ) as Amount_Person
    FROM tbPerson
    ;Edited by: odie_63 on 24 févr. 2011 09:12

  • Error creating view with CASE -- WHEN statement in SQL*Plus

    I am using Oracle 8i 8.1.7
    I have an Oracle view which uses CASE...WHEN statements.
    The view compiles fine in DBA studio.
    Using TOAD I saved the view as an *.sql file.
    However, when I try to create the view in SQL*Plus I get the following error:
    SP2-0734: unknown command beginning "CASE WHEN ..." - rest of line ignored.
    According to the documentation CASE -- WHEN has been implemented since since Oracle 8i rel. 2 (8.1.6)

    Well I'm using 8.1.6.3 and CASE and DECODE both work for me:
    SQL> create or replace view v_accs as select account_name, txn,
    2 decode(credit, 0, 'DB', 'CR') t_type
    3 from accs;
    View created.
    SQL> select * from v_accs;
    ACCOUNT_NA TXN T_
    APC 1 DB
    ABC 2 DB
    HJJ 3 DB
    HJH 4 CR
    HJK 5 CR
    APC 6 DB
    APC 7 DB
    ABC 8 DB
    ABC 9 DB
    HJJ 10 DB
    HJJ 11 DB
    HJH 12 DB
    HJH 13 DB
    HJK 14 DB
    HJK 15 CR
    15 rows selected.
    SQL> create or replace view v_accs as select account_name, txn,
    2 case when credit = 0 then 'DB' else 'CR'end as t_type
    3* from accs
    View created.
    SQL> select * from v_accs;
    ACCOUNT_NA TXN T_
    APC 1 DB
    ABC 2 DB
    HJJ 3 DB
    HJH 4 CR
    HJK 5 CR
    APC 6 DB
    APC 7 DB
    ABC 8 DB
    ABC 9 DB
    HJJ 10 DB
    HJJ 11 DB
    HJH 12 DB
    HJH 13 DB
    HJK 14 DB
    HJK 15 CR
    15 rows selected.
    SQL>
    rgds, APC

Maybe you are looking for

  • Are these Chargers good for an iPod?

    Hey everyone. Listen, today I went in town and had some time to buy some stuff. Now i needed a charger for my iPod Touch 4G. I didn't buy the one from apple with 30 euro, i just got something else, that should do the same job. But I'm a bit unsure th

  • Using Custom RTP Payloads

    Hello, I need to use my Custom Video Codecs in a media application so as to transform the content before transmitting it and to transform back such content when receiving. I also need to use some elements located in a file. If I get them inside the c

  • SHA1 on torrent info

    Hi all ! I would be greatful if you can help me ! I want to query HTTP tracker server of the bittorrent protocol. In order to do this, I need a SHA1 20 byte hash of the value of the info dictionary from the metadata .torrent file! ANY or advice is VE

  • Lens Distortion Correction Documentation?

    I have seen in some discussions that LR corrects lens distortion for some lenses and some cameras.  I have not been able to find documentation that indicates what cameras and lenses are supported for this - either for LR2.3 or 3.0beta.  Is that docum

  • Recurring "invalid url" message on trusted websites

    I have an iMac and use Firefox and Safari browsers. This problem happens with both. Everything will be fine then I try to navigate to a website, or within a website, and I get something like this: Invalid URL The requested URL "/ws/eBayISAPI.dll?MyEb