MDX - Simple Case Statement

For some reason I am getting an error on a Simple Case Statement
Error (1260052) – Syntax error in input MDX query on line 2 at token ‘)’
Case
When IS(Time.[JAN]) THEN Time.[Feb]
ELSE
Missing
End
Any suggestions.

Hi all,
I have a similar problem as i'm not too sure about the correct syntax I should use
CASE
WHEN IS(Time.CurrentMember, Descendants[2007]) THEN .......
ELSE
MISSING
END
How do I incorporate the check for Time.CurrentMember = Descendants of 2007???
Many thanks!

Similar Messages

  • MDX simple case statement not working?

    hi all - any idea what is wrong with this MDX statement? it is returning blank. I am trying to add a calculated measure using the below code but it is not working. thanks for the help.
    CASE WHEN [Accounts].[Account Name].CURRENTMEMBER = "Cash" THEN
    ([Dates].[Hierarchy].currentMember.lastChild, [Measures].[Measures].[Amount]) END

    If you are checking for the 'Cash' member of the Account Name hierarchy, do you need to do something like this?
    CASE WHEN [Accounts].[Account Name].CURRENTMEMBER IS [Accounts].[Account Name].[Cash] THEN([Dates].[Hierarchy].currentMember.lastChild, [Measures].[Measures].[Amount]) END
    Regards,
    MrHH

  • CASE STATEMENTS AND CASE EXPRESSIONS IN ORACLE9I PL/SQL

    제품 : PL/SQL
    작성날짜 : 2001-11-13
    CASE STATEMENTS AND CASE EXPRESSIONS IN ORACLE9I PL/SQL
    =======================================================
    PURPOSE
    아래의 자료는 Case 문에서 oracle 8.1.7과 Oracle 9i의 New Feature로 8.1.7에서는
    sqlplus 에서만 가능했고, 9i 부터는 pl/sql 까지 가능하다.
    Explanation
    1. Oracle 8.1.7 Feature
    Oracle 8.1.7 에서 Case 문은 Decode 문과 유사하지만, 기존의 decode 문을 쓰는 것보다
    더 많은 확장성과 Logical Power와 좋은 성능을 제공한다. 주로 나이와 같이 category 별로
    나눌때 주로 사용하고 Syntex는 아래와 같다.
    CASE WHEN <cond1> THEN <v1> WHEN <cond2> THEN <v2> ... [ELSE <vn+1> ] END
    각각의 WHEN...THEN 절의 argument 는 255 까지 가능하고 이 Limit를 해결하려면
    Oracle 8i Reference를 참조하면 된다.
    The maximum number of arguments in a CASE expression is 255, and each
    WHEN ... THEN pair counts as two arguments. To avoid exceeding the limit of 128 choices,
    you can nest CASE expressions. That is expr1 can itself be a CASE expression.
    Case Example : 한 회사의 모든 종업원의 평균 봉급을 계산하는데 봉급이 $2000보다 작은경우
    2000으로 계산을 하는 방법이 pl/sql을 대신하여 case function을 사용할 수 있다.
    SELECT AVG(CASE when e.sal > 2000 THEN e.sal ELSE 2000 end) FROM emp e;
    Case Example : 나이를 column으로 가지고 있는 customer table을 예로 들어보자.
    SQL> SELECT
    2 SUM(CASE WHEN age BETWEEN 70 AND 79 THEN 1 ELSE 0 END) as "70-79",
    3 SUM(CASE WHEN age BETWEEN 80 AND 89 THEN 1 ELSE 0 END) as "80-89",
    4 SUM(CASE WHEN age BETWEEN 90 AND 99 THEN 1 ELSE 0 END) as "90-99",
    5 SUM(CASE WHEN age > 99 THEN 1 ELSE 0 END) as "100+"
    6 FROM customer;
    70-79 80-89 90-99 100+
    4 2 3 1
    1 SELECT
    2 (CASE WHEN age BETWEEN 70 AND 79 THEN '70-79'
    3 WHEN age BETWEEN 80 and 89 THEN '80-89'
    4 WHEN age BETWEEN 90 and 99 THEN '90-99'
    5 WHEN age > 99 THEN '100+' END) as age_group,
    6 COUNT(*) as age_count
    7 FROM customer
    8 GROUP BY
    9 (CASE WHEN age BETWEEN 70 AND 79 THEN '70-79'
    10 WHEN age BETWEEN 80 and 89 THEN '80-89'
    11 WHEN age BETWEEN 90 and 99 THEN '90-99'
    12* WHEN age > 99 THEN '100+' END)
    SQL> /
    AGE_G AGE_COUNT
    100+ 1
    70-79 4
    80-89 2
    90-99 3
    Example
    2. Oracle 9i Feature
    Oracle 9i부터는 pl/sql에서도 case문을 사용할 수 있으면 이것은
    복잡한 if-else 구문을 없애고, C언어의 switch문과 같은 기능을 한다.
    아래의 9i pl/sql Sample 및 제약 사항을 보면 아래와 같다.
    Sample 1:
    A simple example demonstrating the proper syntax for a case
    statement
    using a character variable as the selector. See the section entitled
    'Restrictions' at the end of this article for details on which PLSQL
    datatypes may appear as a selector in a case statement or
    expression.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    achar char(1) := '&achar';
    begin
    case achar
    when 'A' then dbms_output.put_line('The description was Excellent');
    when 'B' then dbms_output.put_line('The description was Very Good');
    when 'C' then dbms_output.put_line('The description was Good');
    when 'D' then dbms_output.put_line('The description was Fair');
    when 'F' then dbms_output.put_line('The description was Poor');
    else dbms_output.put_line('The description was No such Grade');
    end case;
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 2:
    A simple example demonstrating the proper syntax for a case
    expression
    using a character variable as the selector. See the section entitled
    'Restrictions' at the end of this article for details on which PLSQL
    datatypes may appear as a selector in a case statement or
    expression.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    achar char(1) := '&achar';
    description varchar2(20);
    begin
    description :=
    case achar
    when 'A' then 'Excellent'
    when 'B' then 'Very Good'
    when 'C' then 'Good'
    when 'D' then 'Fair'
    when 'F' then 'Poor'
    else 'No such grade'
    end;
    dbms_output.put_line('The description was ' || description);
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    NOTE: The above simple samples demonstrate two subtle differences in the
    syntax
    required for case statements and expressions.
    1) A case STATEMENT is terminated using the 'end case' keywords; a
    case
    EXPRESSION is terminated using only the 'end' keyword.
    2) Each item in a case STATEMENT consists of one or more
    statements, each
    terminated by a semicolon. Each item in a case expression
    consists of
    exactly one expression, not terminated by a semicolon.
    Sample 3:
    Sample 1 demonstrates a simple case statement in which the selector
    is
    compared for equality with each item in the case statement body.
    PL/SQL
    also provides a 'searched' case statement as an alternative; rather
    than
    providing a selector and a list of values, each item in the body of
    the
    case statement provides its own predicate. This predicate can be any
    valid boolean expression, but only one case will be selected.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    achar char(1) := '&achar';
    begin
    case
    when achar = 'A' then dbms_output.put_line('The description was
    Excellent');
    when achar = 'B' then dbms_output.put_line('The description was Very
    Good');
    when achar = 'C' then dbms_output.put_line('The description was
    Good');
    when achar = 'D' then dbms_output.put_line('The description was
    Fair');
    when achar = 'F' then dbms_output.put_line('The description was
    Poor');
    else dbms_output.put_line('The description was No such Grade');
    end case;
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 4:
    This sample demonstrates the proper syntax for a case expression of
    the
    type discussed in Sample 3 above.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    achar char(1) := '&achar';
    description varchar2(20);
    begin
    description :=
    case
    when achar = 'A' then 'Excellent'
    when achar = 'B' then 'Very Good'
    when achar = 'C' then 'Good'
    when achar = 'D' then 'Fair'
    when achar = 'F' then 'Poor'
    else 'No such grade'
    end;
    dbms_output.put_line('The description was ' || description);
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 5:
    This sample demonstrates the use of nested case statements. It is
    also
    permissable to nest case expressions within a case statement (though
    it
    is not demonstrated here), but nesting of case statements within a
    case
    expression is not possible since statements do not return any value.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    anum1 number := &anum1;
    anum2 number := &anum2;
    answer number;
    begin
    case anum1
    when 1 then case anum2
    when 1 then answer := 10;
    when 2 then answer := 20;
    when 3 then answer := 30;
    else answer := 999;
    end case;
    when 2 then case anum2
    when 1 then answer := 15;
    when 2 then answer := 25;
    when 3 then answer := 35;
    else answer := 777;
    end case;
    else answer := 555;
    end case;
    dbms_output.put_line('The answer is ' || answer);
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 6:
    This sample demonstrates nesting of case expressions within another
    case
    expression. Note again the absence of semicolons to terminate both
    the
    nested case expression and the individual cases of those
    expressions.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    declare
    anum1 number := &anum1;
    anum2 number := &anum2;
    answer number;
    begin
    answer :=
    case anum1
    when 1 then case anum2
    when 1 then 10
    when 2 then 20
    when 3 then 30
    else 999
    end
    when 2 then case anum2
    when 1 then 15
    when 2 then 25
    when 3 then 35
    else 777
    end
    else 555
    end;
    dbms_output.put_line('The answer is ' || answer);
    end;
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Although PL/SQL anonymous blocks have been used in all of the examples
    so far,
    case statements and expressions can also be used in procedures,
    functions, and
    packages with no changes to the syntax.
    The following samples are included for completeness and demonstrate the
    use of
    case statements and/or expressions in each of these scenarios.
    Sample 7:
    This sample demonstrates use of a case statement in a stored
    procedure.
    Note that this sample also demonstrates that it is possible for each
    of
    the items in the case body to consist of more than one statement.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    create or replace procedure testcasestmt ( anum IN number ) is
    begin
    case
    when anum = 1 then dbms_output.put_line('The number was One');
    dbms_output.put_line('In case 1');
    when anum = 2 then dbms_output.put_line('The number was Two');
    dbms_output.put_line('In case 2');
    when anum = 3 then dbms_output.put_line('The number was Three');
    dbms_output.put_line('In case 3');
    when anum = 4 then dbms_output.put_line('The number was Four');
    dbms_output.put_line('In case 4');
    when anum = 5 then dbms_output.put_line('The number was Five');
    dbms_output.put_line('In case 5');
    else dbms_output.put_line('The description was Invalid input');
    dbms_output.put_line('In the else case');
    end case;
    end;
    exec testcasestmt(&anum);
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 8:
    This sample demonstrates the use of a case statement in a stored
    package.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    create or replace package testpkg2 is
    procedure testcasestmt ( anum IN number );
    function testcasestmt_f ( anum IN number ) return number;
    end testpkg2;
    create or replace package body testpkg2 is
    procedure testcasestmt ( anum IN number ) is
    begin
    case
    when anum = 1 then dbms_output.put_line('The number was One');
    dbms_output.put_line('In case 1');
    when anum = 2 then dbms_output.put_line('The number was Two');
    dbms_output.put_line('In case 2');
    when anum = 3 then dbms_output.put_line('The number was Three');
    dbms_output.put_line('In case 3');
    when anum = 4 then dbms_output.put_line('The number was Four');
    dbms_output.put_line('In case 4');
    when anum = 5 then dbms_output.put_line('The number was Five');
    dbms_output.put_line('In case 5');
    else dbms_output.put_line('The description was Invalid input');
    dbms_output.put_line('In the else case');
    end case;
    end;
    function testcasestmt_f ( anum IN number ) return number is
    begin
    case
    when anum = 1 then dbms_output.put_line('The number was One');
    dbms_output.put_line('In case 1');
    when anum = 2 then dbms_output.put_line('The number was Two');
    dbms_output.put_line('In case 2');
    when anum = 3 then dbms_output.put_line('The number was Three');
    dbms_output.put_line('In case 3');
    when anum = 4 then dbms_output.put_line('The number was Four');
    dbms_output.put_line('In case 4');
    when anum = 5 then dbms_output.put_line('The number was Five');
    dbms_output.put_line('In case 5');
    else dbms_output.put_line('The description was Invalid input');
    dbms_output.put_line('In the else case');
    end case;
    return anum;
    end;
    end testpkg2;
    exec testpkg2.testcasestmt(&anum);
    variable numout number
    exec :numout := testpkg2.testcasestmt_f(&anum);
    print numout
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    Sample 9:
    This sample demonstrates the use of a case expression in a stored
    package.
    - - - - - - - - - - - - - - - - Code begins here - - - - - - - - - - - -
    set serveroutput on
    create or replace package testpkg is
    procedure testcase ( anum IN number );
    function testcase_f ( anum IN number ) return number;
    end testpkg;
    create or replace package body testpkg is
    procedure testcase ( anum IN number ) is
    anumber number := anum;
    anothernum number;
    begin
    anothernum :=
    case
    when anumber = 1 then anumber + 1
    when anumber = 2 then anumber + 2
    when anumber = 3 then anumber + 3
    when anumber = 4 then anumber + 4
    when anumber = 5 then anumber + 5
    else 999
    end;
    dbms_output.put_line('The number was ' || anothernum);
    end;
    function testcase_f ( anum IN number ) return number is
    anumber number := anum;
    anothernum number;
    begin
    anothernum :=
    case
    when anumber = 1 then anumber + 1
    when anumber = 2 then anumber + 2
    when anumber = 3 then anumber + 3
    when anumber = 4 then anumber + 4
    when anumber = 5 then anumber + 5
    else 999
    end;
    dbms_output.put_line('The number was ' || anothernum);
    return anothernum;
    end;
    end testpkg;
    variable numout number
    exec testpkg.testcase(&anum);
    exec :numout := testpkg.testcase_f(&anum);
    print numout
    - - - - - - - - - - - - - - - - Code ends here - - - - - - - - - - - -
    제약 사항
    다음의 databasetype은 case 문에서 지원되지 않는다.
    BLOB
    BFILE
    VARRAY
    Nested Table
    PL/SQL Record
    PL/SQL Version 2 tables (index by tables)
    Object type (user-defined type)
    All of these types except for object types face a similar restriction
    even for if statements (i.e. they cannot be compared for equality directly) so this is unlikely to change for these types. Lack of support for object types is simply an implementation restriction which may be relaxed in future releases.
    Reference Ducumment
    Oracle 8.1.7 Manual
    NOTE:131557.1

    I have done the following code but doesn't
    like the statement of - "case(butNext)". What do you mean "doesn't like" -- did you get an error message?
    I'm guessing it won't compile because you're trying to switch on a Button.
    I tried something
    like "g.fillOval(100,50,70,90, BorderLayout.NORTH)"...no that doesn't make sense. You only use BorderLayout.NORTH when you're adding components to a BorderLayout layout manager. An oval is not a component and fillOval isn't adding a component and Graphics is not a Panel or layout manager.
    Would appreciate it if someone could tell me how to position
    shapes using the graohic method. I think the problem is that you're confusing shapes with components.

  • Case Statement Issue

    Hi All,
    I need help figuring out a simple case statement. I need to add an OR condition to a couple of the statements and also have a condition that will return everyting. The first query works but lacks the OR condtion/statement. Any help is appreciated.
    Select item_no
    from item
    where item_id =
    case
    when &p_item = '123' then '123'
    when &p_item = '133' then '133'
    when &p_item = '444' then '444'
    end
    How can i do the following?
    Select item_no
    from item
    where item_id =
    case
    when &p_item = '123' then '123' or null
    when &p_item = '133' then '133' or null
    when &p_item = '444' then '444'
    when &p_item = '999' then return all records
    end

    Hi,
    Like Raghu, I wouldn't use a CASE espression. You can put conditions in a a WHERE clause jsut fione without CASE.
    For example:
    SELECT  item_no
    FROM     item
    WHERE     (     '&p_item'     IN ('123', '133')
         AND     '&p_item'     = NVL ( item_id
                              , '&p_item'
    OR     (     '&p_item'     IN ('444')
         AND     '&p_item'     = item_id
    OR     '&p_item'     IN ('999')
    OR     '&p_item'     IS NULL
    ;If you really, really want to use a CASE exprtession, then you can do something like this:
    SELECT  item_no
    FROM     item
    WHERE     CASE
             WHEN  '&p_item'     IN ('123', '133')
             AND       '&p_item,'     = NVL ( item_id
                              , '&p_item'
                 THEN  '123, 133'
             WHEN  '&p_item'     IN ('444')
             AND       '&p_item'     = item_id
                 THEN  '444'
             WHEN  '&p_item'     IN ('999')
                 THEN  '999'
             WHEN  '&p_item'     IS NULL
                   THEN  'NO P_ITEM'
               ELSE  NULL
            END     IS NOT NULL
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data, for each of a few values of &p_item..
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on Sep 15, 2011 12:29 PM

  • Case statement doubt

    Hi,
    I need some help with a simple case statement. lets say i have a table called name which has following 2 rows -
    ID first_name last_name
    1 JOHN SMITH
    2 JOHN
    i want to write a statement like :-
    select id,case when first_name like '%JO%' then 'John in first_name'
    when last_name like '%SM%' then 'Smith in last_name'
    end result from sample_table;
    The result displayed is -
    1 John in first_name
    2 John in first_name
    how can i rewrite this query to also include a 3rd row which will display the result as -
    1 John in first_name
    2 John in first_name
    1 Smith in last_name
    I want to display this 3rd row which displays last name also.
    Please help.

    974647 wrote:
    Hi,
    I need some help with a simple case statement. lets say i have a table called name which has following 2 rows -
    ID first_name last_name
    1 JOHN SMITH
    2 JOHN
    i want to write a statement like :-
    select id,case when first_name like '%JO%' then 'John in first_name'
    when last_name like '%SM%' then 'Smith in last_name'
    end result from sample_table;
    The result displayed is -
    1 John in first_name
    2 John in first_name
    how can i rewrite this query to also include a 3rd row which will display the result as -
    1 John in first_name
    2 John in first_name
    1 Smith in last_name
    I want to display this 3rd row which displays last name also.
    Please help.Hi,
    welcome to the forum.
    Please read SQL and PL/SQL FAQ
    Additionally when you put some code please enclose it between two lines starting with {noformat}{noformat}
    i.e.:
    {noformat}{noformat}
    SELECT ...
    {noformat}{noformat}
    What you want to achieve cannot be done with a single query without using some tricks.
    Let's start from the point that your table has 2 rows. If you make a query without using a filter (A WHERE clause) and without using any CONNECT BY clause the number of rows returned by your query will be 2.
    So a possible solution is to make a union of the same table applying the condition to select only records that you want to display.
    i.e.:
    First I select only records having first_name like '%JO%' then I make a union with all rows having last_name like '%SM%'WITH sample_table AS
    SELECT 1 id, 'JOHN' first_name, 'SMITH' last_name FROM DUAL UNION ALL
    SELECT 2 id, 'JOHN' first_name, NULL last_name FROM DUAL
    SELECT id, 'John in first_name' txt
    FROM sample_table
    WHERE first_name like '%JO%'
    UNION ALL
    SELECT id, 'Smith in last_name' txt
    FROM sample_table
    WHERE last_name like '%SM%';
    ID TXT
    1 John in first_name
    2 John in first_name
    1 Smith in last_name
    Regards.
    Al
    Edited by: Alberto Faenza on Dec 3, 2012 5:27 PM
    Removed case, no need                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • MDX -Children count function Not working in Case statement

    Hi,
    I am trying to create set when you slice with the Hierarchy member is leaf level , I want a output only that Leaf level .
    and When I slice with  the parent level , it has to give all the members below that parent level.
    But the problem here is when I select child member or leaf member , The first condition in the Case is not working
    WITH SET
    TESTSET AS
    CASE
    WHEN
     [Dimension].[Hierarchy].currentmember.children.count<0
    THEN
     [Dimension].[Hierarchy].currentmember
    ELSE
     DESCENDANTS([Dimension].[Hierarchy].Currentmember,,AFTER)
    END
    SELECT
    WBSSET ON 1,
    {} on 0
    FROM
     (SELECT {[Dimension].[Hierarchy].&[10]} ON COLUMNS FROM [CubeName])
    Thanks,
    Santosh

    Hi Santosh,
    I don't think Children count function not working in case statement, I have tested it on my local environment, here s the sample query for you reference.
    with member
    testset as
    case
    when
    [Geography].[Geography].currentmember.children.count<10
    then "X"
    else "OK"
    end
    select testset on 0,
    {[Geography].[Geography].[Country].members} on 1
    from
    [Adventure Works]
    In your scenario, the issue might be caused by the query isself, you can try to use IsLeaf Funcion to achieve your requirement. Please refer to the links below.
    http://msdn.microsoft.com/en-us/library/ms144932.aspx
    http://www.databasejournal.com/features/mssql/article.php/3633696/MDX-Operators-The-IsLeaf-Operator--Conditional-Logic-within--Calculations.htm
    http://www.mdxpert.com/Functions/MDXFunction.aspx?f=22
    Regards,
    Charlie Liao
    TechNet Community Support

  • Can't make simple 'IF' statement work in MS Query!?

    I have read the existing threads on the subject but can't seem to make a simple 'IF' statement work in MS Query with a single table. I always get the following error:
    Returns error message:
    "Incorrect syntax near the keyword 'if'
    Incorrect syntax near ','.
    Statement(s) could not be prepared.
    Here's my query (simplified, but not much):
    select *
    from table t
    where t.modifieddate > if(t.active=0, date1, date2)
    Just to see if I could get AN if statement to work, I've also tried:
    SELECT t.active, if(t.active=0,2,3)
    FROM CdmsTimeSheet.dbo.Registrations t
    And
    SELECT t.active, if(t.active='0','2','3')
    FROM CdmsTimeSheet.dbo.Registrations t
    And
    SELECT t.active,
    FROM CdmsTimeSheet.dbo.Registrations t
    where datemodified>if(t.active='0',3/1/2014,1/1/2014)
    and
    SELECT t.active,
    FROM CdmsTimeSheet.dbo.Registrations t
    where datemodified>if(t.active='0',#3/1/2014#,#1/1/2014#)
    I've been using excel/ms query for many years but not in the last year or two and am wondering if this was somehow removed? I tried using decode but then get a "not built-in function" error.
    Please help! Thanks

    Not sure what kind of database you are using. In SQL Server, you should use 'Case when' Statement Or 'IIF' function instead of if.
    e.g.
    select
    top (10) BusinessEntityID,iif( BusinessEntityID=1 , 'true' , 'false') as test
    from HumanResources.Employee
    order by BusinessEntityID
    Wind Zhang
    TechNet Community Support

  • Using a scalar user defined function in a select case statement

    I have a simple select statement below and I want to have a case statement for some conditions based on the result of the udf returned value. I then want the returned value from that specific case statement to be returned where i have indicated 'DISPLAY
    ZIPCODE' below.
    SELECT
        CASE
             WHEN (SELECT Top 1 ZipCode FROM ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712) IS NOT NULL THEN 'DISPLAY ZIPCODE'
             WHEN (SELECT Top 1 ZipCode FROM ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,714) as r) IS NOT NULL THEN 'DISPLAY ZIPCODE'
             ELSE NULL
           END as Zipcode,
    FROM
    table1

    SELECT COALESCE(ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712),ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712)) AS Zipcode
      FROM table1
    Nope. This is two function calls. coalesce(a, b, c, ...) is just syntatic sugar for
       CASE WHEN a IS NOT NULL THEN a
            WHEN b IS NOT NULL THEN b
            WHEN c IS NOT NULL THEN c
       END
    But if you use isnull it's a different matter. (But isnull() permits two arguments.)
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Problem In Case Statement

    HI ALL,
    in the program,i need to display description based on condition type.
    so we used CASE statement as below in LOOP. ITS not working .
    could anyone please suggest if anything wrong in the following code.
    LOOP AT gt_mat.
           MOVE gt_mat-type  TO gt_final-type.
      Condition description
    CASE gt_mat-type  .
    when  'N'.
    move  'New'  to gt_final-condes.
    when  'B'.
    move 'Block'   to gt_final4-condes.
    ENDCASE.
    ENDLOOP.
    please let me know whats the problem i above code and how to solve.
    thanks in advance.

    Hi,
    The only solution for this could be to debug the program and check if the coresponding values are populated and available. You could also try to replace the CASE statement with a simple IF statement and check if its working.
    LOOP AT gt_mat.
    MOVE gt_mat-type TO gt_final-type.
    * Condition description
    IF gt_mat-type = 'N'.
    gt_final-condes = 'New'.
    elseif gt_mat-type = 'B'.
    gt_final-condes ='Block'.
    append gt_final.
    clear gt_final.
    ENDIF.
    ENDLOOP.
    Also make sure the case of the value in gt_mat-type matches with the condition
    Regards,
    Vikranth

  • Need help in this query using Case Statement

    I have the following query which is currently existing and I am adding few more conditions based on the new requirements which is based on a particular flag set as 1 or 0.
    If it is set to 1 then I should use the old query as it is and if it is set to 0 then I should add the new conditions.
    Basically when the flag is set to 0, I shouldnt be including some of the records that already exists and should include only new records. This is based on the plan_type_ids in (1,2,3,4).
    Hence I am using the Case statement to check if the plan_type_id is in (1,2) then do a set of not exists and if the plan_type_id in (3,4) then do set of not exists.
    But when I run this query it is giving me error. What am I doing wrong?
    Is there any other simple way to combine all the not exists for all of those select statements as given after the line ------------------------- into a single one?
    What am I doing wrong?
    I tried putting the NOT EXists before the case too but that too didnt work.
    Please help. Appreciate it.
    Thank you in advance.
    SELECT
    ee.employee_id
    ,'WELCOMEMSG'
    ,DECODE( me.member_enrollment_id
    ,first_enr.enrollment_id
    ,20
    ,23
    ) status_id
    ,me.member_enrollment_id
    ,wk.welcome_msg_id
    FROM wk
    ,employees ee
    ,MEMBER_ENROLLMENTS me
    ,plans pl
    ,( SELECT employee_id
    ,plan_type_id
    ,start_date
    ,plan_id
    ,MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
    FROM ( SELECT me.employee_id
    ,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
    ,pl.start_date
    ,wk.plan_id
    ,me.member_enrollment_id
    FROM wk
    ,PLANS pl
    ,MEMBER_ENROLLMENTS me
    WHERE wk.done_by = nvl('TEST' ,wk.done_by)
    AND wk.welcome_msg_name <> 'NONE'
    AND pl.employer_id = wk.employer_id
    AND wk.employer_id = 5
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    MINUS
    SELECT me.employee_id
    ,DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id
    ,pl.start_date
    ,NULL plan_id
    ,me.member_enrollment_id
    FROM wk
    ,PLANS pl
    ,MEMBER_ENROLLMENTS me
    WHERE wk.done_by = nvl(NULL,wk.done_by)
    AND wk.welcome_msg_name <> 'NONE'
    AND pl.employer_id = wk.employer_id
    AND wk.employer_id = 5
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id = wk.plan_id
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    WHERE employee_id = 100
    GROUP BY employee_id ,plan_type_id,start_date ,plan_id
    )first_enr
    ,MEMBER_EVENTS mv
    WHERE wk.done_by = nvl(NULL,wk.done_by)
    AND wk.employer_id = ee.employer_id
    AND ee.employee_id = me.employee_id
    AND ee.employee_id = 100
    AND me.plan_id = pl.plan_id
    AND me.coverage_effective_date <> NVL(me.coverage_end_Date, me.coverage_effective_date + 1)
    AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
    AND is_expired(me.employee_id,me.plan_id) = 'Y'
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
    AND pl.plan_id=nvl(wk.plan_id,pl.plan_id)
    AND me.employee_id = first_enr.employee_id
    AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
    AND pl.start_date = first_enr.start_date
    AND me.member_enrollment_id = mv.member_enrollment_id (+)
    AND 'WELCOMEMSG' = mv.event_name(+)
    AND mv.member_enrollment_id IS NULL
    AND wk.welcome_msg_name <> 'NONE'
    AND NVL(first_enr.plan_id,0) = NVL( wk.plan_id,0)
    AND (FN_get_all_participant(wk.employer_id) = 1
    OR
    (FN_get_all_participant(wk.employer_id) = 0
    AND (CASE WHEN pl.plan_type_id IN (1,2)
    THEN NOT EXISTS (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = me.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl.START_DATE
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id in (1, 2)
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = me.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date = (pl.start_date - 365)
    \ AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = wk.plan_type_id
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = 2
    UNION
    (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id = 1
    WHEN pl.plan_type_id IN (3, 4)
    THEN NOT EXISTS (SELECT 'X'
    FROM member_accounts ma
    member_enrollments men3
    plans pl3
    plan_types pt3
    WHERE ma.member_account_id = men3.member_account_id
    AND ma.employee_id = men2.employee_id
    AND nvl(ma.account_end_date, sysdate) <= trunc(sysdate)
    AND ma.plan_id = pl3.plan_id
    AND pl3.start_date <= pl2.START_DATE
    AND TRUNC(men3.coverage_effective_date) <> TRUNC(NVL(men3.coverage_end_date, men3.coverage_effective_date + 1 ))
    AND pl3.plan_type_id = pt3.plan_type_id
    AND pt3.plan_type_id in (3, 4)
    END
    AND (CASE WHEN pl.plan_type_id IN (1,2)
    ERROR at line 89:
    ORA-00936: missing expression

    Maybe
    SELECT ee.employee_id,
           'WELCOMEMSG',
           DECODE(me.member_enrollment_id,first_enr.enrollment_id,20,23) status_id,
           me.member_enrollment_id,
           wk.welcome_msg_id
      FROM wk,
           employees ee,
           MEMBER_ENROLLMENTS me,
           plans pl,
           (SELECT employee_id,
                   plan_type_id,
                   start_date,
                   plan_id,
                   MIN(MEMBER_ENROLLMENT_ID) member_enrollment_id
              FROM (SELECT me.employee_id,
                           DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
                           pl.start_date,
                           wk.plan_id,
                           me.member_enrollment_id
                      FROM wk,
                           PLANS pl,
                           MEMBER_ENROLLMENTS me
                     WHERE wk.done_by = nvl('TEST',wk.done_by)  /* same as wk.done_by = 'TEST' */
                       AND wk.welcome_msg_name 'NONE'
                       AND pl.employer_id = wk.employer_id
                       AND wk.employer_id = 5
                       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
                       AND pl.plan_id = NVL(wk.plan_id,pl.plan_id)
                       AND me.plan_id = pl.plan_id
                       AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
                       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date,me.coverage_effective_date + 1)
                    MINUS
                    SELECT me.employee_id,
                           DECODE(pl.plan_type_id,1,2,pl.plan_type_id) plan_type_id,
                           pl.start_date,
                           NULL plan_id,
                           me.member_enrollment_id
                      FROM wk,
                           PLANS pl,
                           MEMBER_ENROLLMENTS me
                     WHERE wk.done_by = nvl(NULL,wk.done_by)  /* same as 1 = 1 */
                       AND wk.welcome_msg_name 'NONE'
                       AND pl.employer_id = wk.employer_id
                       AND wk.employer_id = 5
                       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
                       AND pl.plan_id = wk.plan_id
                       AND me.plan_id = pl.plan_id
                       AND me.coverage_effective_date NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
                       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
             WHERE employee_id = 100
             GROUP BY employee_id,
                      plan_type_id,
                      start_date,
                      plan_id
           ) first_enr,
           MEMBER_EVENTS mv
    WHERE wk.done_by = nvl(NULL,wk.done_by)
       AND wk.employer_id = ee.employer_id
       AND ee.employee_id = me.employee_id
       AND ee.employee_id = 100
       AND me.plan_id = pl.plan_id
       AND me.coverage_effective_date != NVL(me.coverage_end_Date,me.coverage_effective_date + 1)
       AND me.coverage_effective_Date BETWEEN wk.start_date AND NVL(wk.end_date, me.coverage_effective_date + 1)
       AND is_expired(me.employee_id,me.plan_id) = 'Y'
       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = wk.plan_type_id
       AND pl.plan_id = nvl(wk.plan_id,pl.plan_id)
       AND me.employee_id = first_enr.employee_id
       AND DECODE(pl.plan_type_id,1,2,pl.plan_type_id) = first_enr.plan_type_id
       AND pl.start_date = first_enr.start_date
       AND me.member_enrollment_id = mv.member_enrollment_id(+)
       AND 'WELCOMEMSG' = mv.event_name(+)
       AND mv.member_enrollment_id IS NULL
       AND wk.welcome_msg_name != 'NONE'
       AND NVL(first_enr.plan_id,0) = NVL(wk.plan_id,0)
       AND (
            FN_get_all_participant(wk.employer_id) = 1
        OR
            (FN_get_all_participant(wk.employer_id) = 0
       AND   NOT EXISTS(SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = me.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date < pl.START_DATE
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id in (1,2)
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = me.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date = (pl.start_date - 365)
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id = wk.plan_type_id
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = men2.employee_id
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date < pl2.START_DATE -- '01-Jan-2011'
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id = (1,2)
                           and pl.plan_type_id IN (1,2)
                        UNION
                        SELECT 'X'
                          FROM member_accounts ma,
                               member_enrollments men3,
                               plans pl3,
                               plan_types pt3
                         WHERE ma.member_account_id = men3.member_account_id
                           AND ma.employee_id = men2.employee_id
                           AND trunc(nvl(ma.account_end_date,sysdate)) <= trunc(sysdate)
                           AND ma.plan_id = pl3.plan_id
                           AND pl3.start_date <= pl2.START_DATE
                           AND TRUNC(men3.coverage_effective_date) != TRUNC(NVL(men3.coverage_end_date,men3.coverage_effective_date + 1))
                           AND pl3.plan_type_id = pt3.plan_type_id
                           AND pt3.plan_type_id in (3,4)
                           and pl.plan_type_id IN (3,4)
           )Regards
    Etbin

  • Case statement in where clause ??

    Hello gurus,
    Can we use case statements in where clause ?? Any example will be great!
    And also i would like to know, besides CASE and DECODE statements, Is there any way we can use IF ELSE statements in SELECT clause or in WHERE clause ?
    Thank you!!

    Hi,
    user642297 wrote:
    Hoek,
    Thanks for the reply
    Whatever you return from 'then' should match your criteria.I didnt get this part...can you elaborate this part ?? Thank you!!Remember what a CASE expression does: it returns a single value in one of the SQL data types (or NULL).
    You're probably familiar with conditions such as
    WHERE   col = 1Inthe example above, col could be replaced by any kind of expression: a function call, and operation (such as "d * 24") or a CASE expression, which is exactly what Hoek posted:
    where  case
             when col = 6 then 1
             when col = 9 then 1
           end = 1;I think what Hoek meant about mnatching was this: since the CASE expression is being compared to a NUMBER, then every THEN clause (as well as the ELSE, if there is one) should return the same data type. You can't have one THEN clause return a NUMBER, and another one in the same CASE expression return a DATE, like this:
    where  case
             when col = 6 then 1
             when col = 9 then SYSDATE     -- WRONG! Raises ORA-00932: inconsistent datatypes
           end = 1; 
    By the way, it's rare when a CASE expression really helps in a WHERE clause. CASE is great for doing conitional stuff in places where you otherwise can't (in the ORDER BY clause, for example), but the WHERE clause was designed for conditions.
    Hoek was just trying to give a simple example. If you really wanted those results, it would be simpler to say:
    where  col = 6
    or     col = 9and simpler still to say
    where  col  IN (6, 9)

  • CASE Statement in IRR Compute(probable bug)

    In IRR i am not able to create Compute with CASE statement. It is giving me error
    "Invalid computation expression. THEN"
    It is simple Compute
    CASE WHEN A = 'SALES' THEN B + C ELSE B END
    Also when i noticed the available functions(in Compute screen) THEN is missing.
    Is it a bug? or I am missing something
    Regards
    Prabahar

    I'm also running into this bug, though we're still back at Apex 3.1.1.
    Has anyone figured out a workaround for it? I tried changing CASE to a DECODE, but I'm still getting an error. I'm having trouble understanding how any computation that used a function would pass this validation test.
    Suggestions would be most welcome.
    Thanks,
    Stew

  • Use of decode/case statements

    I am trying to use a decode or case statement to check for a particular field code of 'SIP' and if that is the value I only want half of the production figure used in the calculations for that field ('SIP').
    The following code works well without the items commented out:
    SELECT ALL
    FINDER_WIS.PRODUCTION_HDR.END_TIME as prod_date,
    'SCD' as district,
    --decode(FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE,'SIP',FINDER_WIS.PRODUCTION_DATA.VOLUME/2,FINDER_WIS.PRODUCTION_DATA) AS TEST_SIP,
    /*case when FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE = 'SIP'
         then FINDER_WIS.PRODUCTION_DATA.VOLUME/2
              else FINDER_WIS.PRODUCTION_DATA.VOLUME
    end as fieldtest,*/
    round(SUM(NVL(FINDER_WIS.PRODUCTION_DATA.VOLUME,0))) as total_oil,
    ROUND(SUM(NVL(FINDER_WIS.PRODUCTION_DATA.VOLUME,0)) / TO_NUMBER(TO_CHAR(FINDER_WIS.PRODUCTION_HDR.END_TIME,'DD'))) AS BOPD
    FROM FINDER_WIS.PRODUCTION_HDR,
    FINDER_WIS.PRODUCTION_DATA,
    FINDER_WIS.FACILITY,
    FINDER_WIS.REPORTING_GROUP,
    FINDER_WIS.REPORTING_GROUP_DETAIL,
    FINDER_WIS.FACILITY_FIELD_X,
    SELECT distinct FINDER_WIS.FACILITY.FACILITY_S
    FROM FINDER_WIS.PRODUCTION_HDR,
    FINDER_WIS.PRODUCTION_DATA,
    FINDER_WIS.FACILITY,
    FINDER_WIS.REPORTING_GROUP,
    FINDER_WIS.REPORTING_GROUP_DETAIL
    WHERE (FINDER_WIS.PRODUCTION_HDR.ACTIVITY_TYPE = 'ALLOCATED'
    AND FINDER_WIS.PRODUCTION_HDR.TIME_PERIOD_TYPE = 'MONTH'
    AND FINDER_WIS.PRODUCTION_HDR.STATE_TYPE = 'STANDARD'
    AND FINDER_WIS.PRODUCTION_HDR.EXISTENCE_TYPE = 'ACTUAL'
    AND FINDER_WIS.PRODUCTION_DATA.MATERIAL_TYPE='OIL'
    and FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_TYPE = 'ASSET_TEAM'
    AND FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_ID ='SCD'
    and finder_wis.production_HDR.SOURCE = 'NEWWIS'
    AND FINDER_WIS.PRODUCTION_HDR.START_TIME BETWEEN :startdate_var AND :enddate_var)
    AND ((FINDER_WIS.PRODUCTION_DATA.PRODUCTION_HDR_S=FINDER_WIS.PRODUCTION_HDR.PRODUCTION_HDR_S)
    and (FINDER_WIS.PRODUCTION_HDR.FACILITY_S = FINDER_WIS.FACILITY.FACILITY_S)
    and (FINDER_WIS.PRODUCTION_HDR.START_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010')
    or FINDER_WIS.PRODUCTION_HDR.end_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010'))
    AND (FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_S = FINDER_WIS.REPORTING_GROUP_DETAIL.REPORTING_GROUP_S)
    AND (FINDER_WIS.FACILITY.FACILITY_S = FINDER_WIS.REPORTING_GROUP_DETAIL.FACILITY_S)))T
    WHERE (FINDER_WIS.PRODUCTION_HDR.ACTIVITY_TYPE = 'ALLOCATED'
    AND FINDER_WIS.PRODUCTION_HDR.TIME_PERIOD_TYPE = 'MONTH'
    AND FINDER_WIS.PRODUCTION_HDR.STATE_TYPE = 'STANDARD'
    AND FINDER_WIS.PRODUCTION_HDR.EXISTENCE_TYPE = 'ACTUAL'
    AND FINDER_WIS.PRODUCTION_DATA.MATERIAL_TYPE='OIL'
    and FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_TYPE = 'ASSET_TEAM'
    and finder_wis.production_HDR.SOURCE = 'NEWWIS'
    AND FINDER_WIS.PRODUCTION_HDR.START_TIME BETWEEN :startdate_var AND :enddate_var)
    AND ((FINDER_WIS.FACILITY.FACILITY_S = T.FACILITY_S)
    AND (FINDER_WIS.PRODUCTION_DATA.PRODUCTION_HDR_S=FINDER_WIS.PRODUCTION_HDR.PRODUCTION_HDR_S)
    AND FINDER_WIS.FACILITY_FIELD_X.UWI = FINDER_WIS.FACILITY.UWI
    AND FINDER_WIS.FACILITY_FIELD_X.FIELD_CODE NOT IN ('MW','BRM','PLG','SIP')
    and (FINDER_WIS.PRODUCTION_HDR.FACILITY_S = FINDER_WIS.FACILITY.FACILITY_S)
    and (FINDER_WIS.PRODUCTION_HDR.START_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010')
    or FINDER_WIS.PRODUCTION_HDR.end_TIME between FINDER_WIS.REPORTING_GROUP_DETAIL.start_time and nvl(FINDER_WIS.REPORTING_GROUP_DETAIL.end_time,'01-JAN-2010'))
    AND (FINDER_WIS.REPORTING_GROUP.REPORTING_GROUP_S = FINDER_WIS.REPORTING_GROUP_DETAIL.REPORTING_GROUP_S)
    AND (FINDER_WIS.FACILITY.FACILITY_S = FINDER_WIS.REPORTING_GROUP_DETAIL.FACILITY_S))
    GROUP BY FINDER_WIS.PRODUCTION_HDR.END_TIME
    the results look like this but this is without the values for the 'SIP' field:
    PROD_DATE     DISTRICT     TOTAL_OIL BOPD
    31/10/2007     SCD     168009     5420
    30/11/2007     SCD     167339     5578
    31/12/2007     SCD     170277     5493
    31/01/2008     SCD     173677     5602
    29/02/2008     SCD     168498     5810
    31/03/2008     SCD     172689     5571
    30/04/2008     SCD     168180     5606
    31/05/2008     SCD     165448     5337
    30/06/2008     SCD     164631     5488
    31/07/2008     SCD     170073     5486
    31/08/2008     SCD     166520     5372
    30/09/2008     SCD     160321     5344
    When I try to add the decode or case statement, I get ORA-00979; not a Group By expression as the error.
    Can anyone assist me with this please?
    Thanks in advance

    Hi and welcome to the forum.
    Simply put the field names you use in your DECODE also in your GROUP BY and it should work:
    simple example:
    MHO%xe> with t as (
      2  select 1 col1, 1 col2 from dual union all
      3  select 1 col1, 1 col2 from dual union all
      4  select 2 col1, 2 col2 from dual union all
      5  select 3 col1, 3 col2 from dual union all
      6  select 4 col1, 4 col2 from dual
      7  )
      8  select col1
      9  ,      decode(col1, 1, col2*100, col2)
    10  ,      sum(col2)
    11  from   t
    12  group by col1 -->> NO col2 here...
    13  order by col1;
    ,      decode(col1, 1, col2*100, col2)
    FOUT in regel 9:
    .ORA-00979: not a GROUP BY expression
    Verstreken: 00:00:05.78
    MHO%xe> with t as (
      2  select 1 col1, 1 col2 from dual union all
      3  select 1 col1, 1 col2 from dual union all
      4  select 2 col1, 2 col2 from dual union all
      5  select 3 col1, 3 col2 from dual union all
      6  select 4 col1, 4 col2 from dual
      7  )
      8  select col1
      9  ,      decode(col1, 1, col2*100, col2)
    10  ,      sum(col2)
    11  from   t
    12  group by col1, col2
    13  order by col1;
          COL1 DECODE(COL1,1,COL2*100,COL2)  SUM(COL2)
             1                          100          2
             2                            2          2
             3                            3          3
             4                            4          4

  • Count Distinct Wtih CASE Statement - Does not follow aggregation path

    All,
    I have a fact table, a day aggregate and a month aggregate. I have a time hierarchy and the month aggregate is set to the month level, the day aggregate is set to the day level within the time hierarchy.
    When using any measures and a field from my time dimension .. the appropriate aggregate is chosen, ie month & activity count .. month aggregate is used. Day & activity count .. day aggregate is used.
    However - when I use the count distinct aggregate rule .. the request always uses the lowest common denominator. The way I have found to get this to work is to use a logical table source override in the aggregation tab. Once I do this .. it does use the aggregates correctly.
    A few questions
    1. Is this the correct way to use aggregate navigation for the count distinct aggregation rule (using the source override option)? If yes, why is this necessary for count distinct .. what is special about it?
    2. The main problem I have now is that I need to create a simple count measure that has a CASE statement in it. The only way I see to do this is to select the Based on Dimensions checkbox which then allows me to add a CASE statement into my count distinct clause. But now the aggregation issue comes back into play and I can't do the logical table source override when the based on dimensions checkbox is checked .. so I am now stuck .. any help is appreciated.
    K

    Ok - I found a workaround (and maybe the preferred solution for my particular issue), which is - Using a CASE Statement with a COUNT DISTINCT aggregation and still havine AGGREGATE AWARENESS
    To get all three of the requirements above to work I had to do the following:
    - Create the COUNT DISTINCT as normal (counting on a USERID physically mapped column in my case)
    - Now I need to map my fact and aggregates to this column. This is where I got the case statement to work. Instead of trying to put the case statement inside of the Aggregate definition by using the checkbox 'Base on Dimension' (which didnt allow for aggregate awareness for some reason) .. I instead specified the case statement in the Column Mapping section of the Fact and Aggregate tables.
    - Once all the LTS's (facts and aggregates) are mapped .. you still have to define the Logical Table Source overrides in the aggregate tab of the count distinct definition. Add in all the fact and aggregates.
    Now the measure will use my month aggregate when i specify month, the day aggregate when i specify day, etc..
    If you are just trying to use a Count Distinct (no CASE satement needed) with Aggregate Awareness, you just need to use the Logical Table Source override on the aggregate tab.
    There is still a funky issue when using the COUNT aggregate type. As long as you dont map multiple logical table sources to the COUNT column it works fine and as expected. But, if you try to add in multiple sources and aggregate awareness it randomly starts SUMMING everything .. very weird. The blog in this thread says to check the 'Based on Dimension' checkbox to fix the problem but that did not work for me. Still not sure what to do on this one .. but its not currently causing me a problem so I will ignore for now ;)
    Thanks for all the help
    K

  • Case statements to count Contact numbers

    Hi,
    I'm trying to use CASE IF or WHEN statements to count simple Contact numbers by owner or region. I need to count the following in separate fields of course:
    Number of contact type = Customers
    Number of Contact types = Propects
    Number of Contacts at each seniority level (6 levels)
    Number of contact validated (tick box)
    I presume I need to write a CASE statement that I can then use to Sum for each column but I'm struggling with CASE statements to apply an CASE IF Column equals 'Customer' then count 1 for example. Never done these before so any help or advice greatly appreciated.
    Contact type field is Contact."Contact Type"
    Contact Seniority is Contact.PICK_3
    Thanks,
    Chris

    here is an example:
    sum (case when contact type = 'Customers' then 1 else 0 end)
    Do the same for the second and forth in your list as above ;
    include the Contact.PICK_3 and # of contact fields in step 1;
    add region, owner, # of Customers, # of Prospects and # of validated in row section of your pivot view;
    add Contact.PICK_3 to column section;
    add # of contact to the measurement section.

Maybe you are looking for

  • How can we download target group from sap crm gui

    Hi Experts, Please let me know, how to download target group from the SAP CRM GUI . Information: TG having 75000 BPs and using sap crm 7.0 Thanks in advance..!! thanks and regards Vinay

  • Configuring IDOC on R/3 and XI ?

    Hi All, I was following one how-to doc in configuration of IDOC on R/3 and XI. If we have more than one scenario with IDOC-XI-File scenarios to handle same or different IDOC's. Where do we configure on R/3 or XI to send IDOC from R/3 to particular Sc

  • PO number when save as Draft

    hi all, PO number is not unique while saving in Draft. I am getting problems in PO number when it is saved as draft and sent for Approval. I have 5 users entering PO at the same time and since POs are being sent for approvals, it is being saved as Dr

  • Can anyone suggest best 500 GB external hard drive for MAC PRO?

    Genius Bar rep told me to back up hard drive before delving into whirring tower... Any suggestions for a 500 GB automatic external hard drive? I've researched several, but they all seem to have drawbacks. Reliability and a quiet fan are priorities. T

  • Preferences lost

    I've had trouble sending some mail. Searched and it appears I need to try Port 587. However, my mail preferences shows only an option to turn spam blocking on or off. There are no other tabs. I can not find a place to choose a port. I have seen a pla