MIN function iside CASE WHEN

Hi gurus,
I am new to PL SQL and wanted to know that cant we put MIN inside the CASE WHEN.
I have following mentioned query:
It is not allowing me to execute it, can anyone help me out?
SELECT A,
B,
D,
E,
F,
SUM
(CASE
WHEN <Expression> THEN (CASE
WHEN ( <Expression>
THEN 0
ELSE min(<Expression>) or min(<Expression>)
     END
ELSE 0
END
) AS sales
FROM tab1
WHERE ( column1 = 'x' )
GROUP BY A,
B,
D,
E,
F,
Edited by: user11001347 on Oct 29, 2009 2:34 AM

Group functions within a case are perfectly acceptable e.g.
select sum(case when dummy = 'X' then '1' else min(dummy) end) from dual group by dummy
SUM(CASEWHENDUMMY='X'THEN'1'ELSEMIN(DUMMY)END)
                                             1There are other problems with your code...
This looks wrong to me:
min(<Expression>) or min(<Expression>)and you have a trailing comma after F in the group by clause.
When you're having problems with a piece of SQL, It's useful to us if you post the error you are getting back, and your table structure and sample test data too if possible.
Edited by: Cyn on Oct 29, 2009 9:43 AM

Similar Messages

  • Evaluate Function in case when

    hi,
    i want to use evaluate function in case when statement.My req is
    if report_date=8/31 and sales_qty>30....show 1
    if report_date=7/15 and sales_qty>39...show 2..
    in the formula section i am using
    case WHEN "Fact- table"."Report  Date"=evaluate('to_date(%1%2)',"Column_name",'DD/MM
    /YYYY')) AND "Fact-table"."Sales_qty">30 THEN '1'  ELSE '0' END Report date data type is date only...Its giving an error date format is not supported
    Please help me out!!

    hi,
    thanks for u r quick response,i tried the code what u sent by its giving the error
    A general error has occurred. [nQSError: 42015] Cannot function ship the following expression: Evaluate( to_char(%1,%2),cast(D1.c7 as DATE ) , 'MM/DD/YYYY') ADa.....
    I tried in sql its giving correct results
    SELECT * FROM table WHERE report_month=To_Date('15/09/2009','dd/mm/yyyy')
    how to use the same in obiee?
    thanks
    Edited by: saichand on Oct 26, 2009 3:17 PM

  • 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

  • CASE WHEN functionality in BI Publisher

    I have previously used Discoverer to do our reporting and am investigating the differences between the two products. I understand that its chalk and cheese but one thing I am having trouble identifying is the CASE WHEN functionality that we use Discoverer in BI Publisher.
    An example of the usage for our CASE WHEN would be creating a derived column in a report to convert existing data into a more standardised format.
    I imagine this is quite a simple answer and I have just missed it but could anyone out there help me with where I can input the functionality in BI publisher.
    I am not the most technically gifted so any answers with simple words help :-)
    Edited by: 943098 on 27-Jun-2012 03:25

    You can use CASE WHEN in a sql query in a data model. You can also use it in the RTF template via "if then else", choose statements etc
    Take a look at section 4.10 in the BI publisher Report Designer's guide for use cases/examples: http://docs.oracle.com/cd/E23943_01/bi.1111/e22254.pdf
    Thanks,
    Bipuser

  • 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

  • SQL statement using result of aggregate min function to fetch a row - how?

    Need help trying to get to something seemingly simple. I'll provide a simple example to illustrate the problem.
    I'll be using a simple addresses table with 4 fields.
    Create the table:
      CREATE TABLE "ADDRESSES"
       (     "OWNER_NAME" VARCHAR2(20 BYTE),
         "STREET_NAME" VARCHAR2(20 BYTE),
         "STREET_NUMBER" NUMBER
       ) ;populate with 6 rows
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('FRED','MAIN',1);
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('JOAN','MAIN',2);
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('JEAN','MAIN',3);
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('JACK','ELM',1);
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('JANE','ELM',2);
    Insert into ADDRESSES (OWNER_NAME,STREET_NAME,STREET_NUMBER) values ('JEFF','ELM',3);We now have this:
    Select * from addresses
    OWNER_NAME           STREET_NAME          STREET_NUMBER         
    FRED                 MAIN                 1                     
    JOAN                 MAIN                 2                     
    JEAN                 MAIN                 3                     
    JACK                 ELM                  1                     
    JANE                 ELM                  2                     
    JEFF                 ELM                  3                     
    6 rows selectedNow i wish to group by StreetName, and get a count of houses. At the same time, i'd like to know the first and last house number
        select
        street_name,
        count(*) "NBR HOUSES",
        min(street_number) "First Number",
        max(street_number) "Last Number"
         from addresses
        group by street_name
    produces
    STREET_NAME          NBR HOUSES             First Number           Last Number           
    ELM                  3                      1                      3                     
    MAIN                 3                      1                      3                     
    2 rows selectedExcellent. Now for the problem. I wish to also list on each line, the owner that lives at the first and/or last house number. Note again, assume that house number and street name is unique
    It would seem i have all that i need. Not sure how to get at it.
    I tried:
    select
        street_name,
        count(*) "NBR HOUSES",
        min(street_number) "First Number",
        max(street_number) "Last Number",
        (Select b.owner_name from addresses b where b.street_number = min(street_number) and b.owner_name = owner_name) "First Owner"
         from addresses
        group by street_nameBut get a sql syntax error, group function not allowed when i add the subselect.
    any ideas?
    thanks for any help.
    Edited by: user6876601 on Nov 19, 2009 7:08 PM
    Edited by: user6876601 on Nov 19, 2009 7:30 PM

    Hi,
    Welcome to the forum!
    Getting the highest and lowest number for each street is a fairly simple concept; simpler to describe, and simple to code.
    Now you want the owner_name associated with the highest and lowest number, which is a more complicated concept; a little harder to describe, and, unfortunately, much less simple to code, and much, much harder to explain:
    select
        street_name,
        count (*)                "NBR HOUSES",
        min (street_number)      "First Number",
        min (owner_name) KEEP (DENSE_RANK FIRST ORDER BY street_number)
                        "First Owner",
        max (street_number)      "Last Number",
        min (owner_name) KEEP (DENSE_RANK LAST ORDER BY street_number)
                        "Last Owner"
         from addresses
        group by street_name
    ;Notice that I used min for both "First Owner" and "Last Owner". Why is that?
    The important keyword in these funtions is the word FIRST or LAST that comes after DENSE_RANK and before ORDER BY. The function at the beginning is merely a tie-breaker.
    That is, MIN in this context only refers to what should happen when there is a tie for the first or last in the group. Even if such a thing is impossibe in your data, the generic functions have to have some mechanism for returning a single owner_name when two or more rows have an equal claim to having the highest street_number. For example, if we change Joan's address to 1 Main, then MIN (street_number) is still 1, obviously, but who is the person associated with the minimum street_number: is it Fred or Joan? Both have an equal claim to being the owner with the lowest address on Main Street, but aggregate functions must return a single value, so we have to have some mechanism for tell the system whether to return 'JOAN' or 'FRED'. In this example, I arbitrarily said that in the een of a tie, the lowest name, in alphabetic order, should be returned. In this case, 'FRED' would be returned, since 'FRED' is earlier than 'JOAN' .
    Thanks for including the CREATE TABLE and INSERT statements!

  • Max & min function

    The max & min function behaves differently in LabVIEW 6.1 compared to 6.0.2. When comparing between NaN and 0.0, the function returned NaN as the max,in 6.0.2. In 6.1, it retunrs 0.0 as the max. Why is this so?

    You should not rely on assumed behavior of functions when treating NaN. You should treat the case explicitly on your diagram.
    By IEEE standard the comparisons
    X > NaN should return FALSE
    X < NaN should also return FALSE.
    Then the output of a MinMax function with a NaN input actually depends on how you compare both inputs.
    using for example
    if (X > Y) then
    Max = X;
    Min = Y;
    else
    Max = Y;
    Min = X;
    end
    This implementation always returns Max = Y and Min = X when comparing with a NaN input because (X > Y) is FALSE.
    On the other hand, the implementation
    if (X < Y) then
    Max = Y;
    Min = X;
    else
    Max = X;
    Min = Y;
    end
    does return swapped values for Min and Max for NaN inputs.
    Usually, primitives don't check
    explicitely for NaN values because it would unecessarly bloat the code. This kind of NaN check is left to the programmer because anyway the output of MinMax function is meaningless when inputs are NaN.
    LabVIEW, C'est LabVIEW

  • SQL Expression in decode function or case statement?

    Can I put SQL expressions in decode function or case statement?
    For example,
    select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
    from element le;
    or
    select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
    ELSE 'Y'
    from element le;
    None of the above work.
    Can anyone tell me how to make it work?
    Is there any workaround?
    Thanks,
    J

    You simply needed and END to your CASE statement;
    SQL> with profile_data as (
       select 'XXXX_AFTER' name, 1 object_id from dual),
         element as (
       select 1 profile from dual union all
       select 2 from dual)
    select le.profile,
       case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
       THEN 'N'
       ELSE 'Y'
       END new_col
    from element le
       PROFILE N
             1 Y
             2 N

  • Hi I installed a new hard drive in my Mac mini osx lion an when I turn it on I get a flashing file with a question mark. I tried holding command and R keys when turning it on but the recovery fails to work. Does any one know how I can get it to recover?

    Hi I installed a new hard drive in my Mac mini osx lion an when I turn it on I get a flashing file with a question mark. I tried holding command and R keys when turning it on but the recovery fails to work. I can hold the option key at start up and choose my network, then Internet recovery shows up with an arrow pointing up. When I click on the arrow Internet recovery fails and all I get is a globe with a triangle on it with an exclamation mark on it, and under that it says
    apple.com/support
          -6002F
    Does any one know how I can fix this without a recovery disc? Thanks

    I just want to add to this, in case someone else searches for this error on Apple Support (google doesnt cover apple support.. how clever is that?)
    I had the same error. And i had a Computer that had worked, with a SSD drive and 16GB upgrade done by the owner himself.
    I tried swapping with a Mechinal Harddrive, no luck.
    Kept the Mechanical drive in, and tried with some other Ram, it worked..
    So for me this error and after reading the other responses can be boiled down to a Harddrive problem or Ram issue.
    It was Ram for me..

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

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

  • Help in changing plsql case when to decode

    Hi,
    Can anyone help me to change this sql for it to use decode function instead of case when? Below is the sql code
    Thanks in advance.
    SELECT
    parts,
    weeks,
    SUM(qty) qty
    FROM (
    SELECT
    CASE
    WHEN ((is_tbd = 'yes' AND is_tbd_order = 'no') OR ex_fac_date > v_asofdate + 131) THEN 'tbd'
    WHEN ex_fac_date BETWEEN v_asofdate - 1 AND v_asofdate + 5 AND is_tbd = 'no' THEN 'wk1'
    WHEN ex_fac_date BETWEEN v_asofdate + 6 AND v_asofdate + 12 AND is_tbd = 'no' THEN 'wk2'
    WHEN ex_fac_date BETWEEN v_asofdate + 13 AND v_asofdate + 19 AND is_tbd = 'no' THEN 'wk3'
    WHEN ex_fac_date BETWEEN v_asofdate + 20 AND v_asofdate + 26 AND is_tbd = 'no' THEN 'wk4'
    WHEN (ex_fac_date < v_asofdate - 1) AND is_tbd = 'no' THEN 'past_due'
    END weeks,
    ffdate,
    parts,
    SUM(qty) qty
    FROM
    delivery)

    I can't use case because my oracle is 8i,Please join 21st Century
    Can you help me checnge this to if then elsif insteadSQL does does contain IF statement!

  • DB Function errors out when trying to use in both Answers and RPD file.

    Hi All,
    Its a strange behaviour that one function which is in Database works fine the other does not. I have created 2 DB functions and accessing them from OBIEE in both Answers and RPD. One function which returns a Date is working fine, the one which is returning a number is throwing a error.
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S0022 code: 904 message: [Oracle][ODBC][Ora]ORA-00904: "APPS"."XXX_BUSINESS_DAYS": invalid identifier. [nQSError: 16001] ODBC error state: S0022 code: 904 message: [Oracle][ODBC][Ora]ORA-00904: "APPS"."XXX_BUSINESS_DAYS": invalid identifier. [nQSError: 16015] SQL statement execution failed. (HY000)
    SQL Issued: SELECT "Soft cost Fact".Project saw_0, "Soft cost Fact"."Fiscal Year" saw_1, "Soft cost Fact".XXX_BUSINESS_DATE saw_2, CASE WHEN EVALUATE('APPS.XXX_BUSINESS_DAYS(%1,%2,%3)',"Soft cost Fact"."Soft Cost Date","Soft cost Fact"."Soft Cost Date",1) =0 THEN 'ZERO' ELSE 'NON-ZERO' END saw_3 FROM "Soft Cost" WHERE "Soft cost Fact".Project = '060096' ORDER BY saw_0, saw_1, saw_2, saw_3
    Both functions are in same schema, have the similar grants.
    Please help me resolving this error. Any pointers in resolving this is much appreciated.
    Thanks,
    Vijay

    I noticed that you have not specified the type of return value that the function returns. It is good practice to do so..
    EVALUATE('DB_Function(%1)' as returntype, {Comma separated Expression}).
    Try EVALUATE('APPS.XXX_BUSINESS_DAYS(%1,%2,%3)' as number,"Soft cost Fact"."Soft Cost Date","Soft cost Fact"."Soft Cost Date",1) to see if that resolves the error.
    Also check this out if you haven't already.
    http://obiee101.blogspot.com/2007/12/evaluate-function-and-presentation.html
    Hope this helps.Thanks.

  • Discoverer: "CASE WHEN...." in calculations won't show anything in GUI !?!?

    Hi all!
    I have a report in Discoverer Plus (Version 10.1.2.48.18) which contains 2 columns: One with actual spendings and one with budget figures. I want to make a third column which holds actual spendings in percentage of the budget. To do this, I need to make a calculation similar to:
    CASE WHEN SUM(budget) <> 0 THEN SUM(spendings)/SUM(budget) ELSE NULL END
    However, when I apply this calculation to the third column, my report don't return any numbers at all in any column.
    What am I doing wrong? -- Is this a bug, and how should I solve my problem?
    ~Morten

    I can get something out if I do this (applying an aggregate function to it all):
    SUM(CASE WHEN budget <> 0 THEN spendings/budget ELSE NULL END)
    However, this is wrong (Summarizing these percentages doesn't give any meaning).
    SQL lookes something like this:
    SELECT /*+ NOREWRITE */ o100448.ACCOUNT as E100451,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) as E101004,MAX(o100448.ACCOUNTNUM) as as100473_100451_NEW,CASE WHEN ( SUM(o100862.BUDGET) ) <> 0 THEN ( SUM(o100862.AMOUNT) )/( SUM(o100862.BUDGET) ) ELSE NULL END as C_1,( SUM(o100862.BUDGET) )*o100448.SIGN as C_3,( SUM(o100862.AMOUNT) )*o100448.SIGN as C_2,GROUPING_ID(o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM')))) as GID
    FROM TANGO.TANGO_ACCOUNTS o100448,
    TANGO.TANGO_SUMS o100862
    WHERE ( (o100862.ORG = o100448.ORG AND o100862.ACCOUNTNUM = o100448.SUBACCOUNTNUM))
    AND (o100448.ACCOUNTNUM BETWEEN 30000 AND 79999)
    AND (o100862.DIM = '50')
    AND (o100448.ORG = 'bru')
    GROUP BY GROUPING SETS(( o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ),( o100448.ACCOUNT,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ))
    HAVING (GROUP_ID()=0)
    ORDER BY GID DESC;
    I tried to fire this SQL up in TOAD (or whatever SQL-tool you might have), and columns C_2 and C_3 are empty. Seems like I'm doing something awfully wrong here.....
    Any ideas? (There must be some SQL sharks out there ;-p)
    ~Morten

  • Another case when statement

    Hi, another case statement question. My set up is calling from dasnboard prompt to publisher with an rtf template. My question is whether my logic/syntax is corrected. Thank you.
    and "office"."branch" = ( CASE WHEN :BRANCH = '' THEN "office"."branch" ELSE :BRANCH END)
    case when :BRANCH is null THEN 'or' ELSE 'and' END -----> (here I want to be able either or / and in case when edit box with BRANCH parameter is null use OR operator and when BRANCH parameter is field use AND operator.
    and "office"."employee" like upper( CASE WHEN :EMPLOYEE = '' THEN "office"."employee" ELSE :EMPLOYEE END)

    try this in TOAD (replace each apostrophe of your sql by two apostrophes and add the starting and ending apostrophe to obtain a string)
    execute immediate 'INSERT INTO DATAMART.PERSONSITE (PERSON_DBID,SITE) SELECT PDBIDSKILL.DBID, MIN(PDBIDSKILL.SKILL) FROM
    (SELECT DISTINCT DATAMART.V_CFG_PERSON.DBID, CASE WHEN DATAMART.V_CFG_SKILL.NAME=''Complex_Rslve'' OR DATAMART.V_CFG_SKILL.NAME=''Broadband_Business_Resolve'' THEN ''BBHD'' WHEN DATAMART.V_CFG_SKILL.NAME=''General'' OR DATAMART.V_CFG_SKILL.NAME=''General_Mobile'' OR DATAMART.V_CFG_SKILL.NAME=''General_Billing'' THEN ''MASS'' WHEN DATAMART.V_CFG_SKILL.NAME=''Faults_General'' OR DATAMART.V_CFG_SKILL.NAME=''Faults_Business'' THEN ''Faults'' ELSE ''_Other'' END AS SKILL
    FROM DATAMART.V_CFG_SKILL INNER JOIN DATAMART.V_CFG_SKILL_LEVEL ON DATAMART.V_CFG_SKILL.DBID = DATAMART.V_CFG_SKILL_LEVEL.SKILL_DBID
    INNER JOIN DATAMART.V_CFG_PERSON ON DATAMART.V_CFG_PERSON.DBID = DATAMART.V_CFG_SKILL_LEVEL.PERSON_DBID) PDBIDSKILL GROUP BY PDBIDSKILL.DBID';it should give the same result as your original sql statement
    before using the string in VBA you must replace '' with the characters used to denote an apostrophe within apostrophes.
    I don't know nothing about VBA but I'm sure you'll know how to deal with it (an apostrophe within apostrophes).
    Regards
    Etbin

Maybe you are looking for