Help Regarding CASE Statement.

hey !
i m trying to apply COUNT to a column , with a particular condition.
like.
CASE
WHEN( A > B)
THEN COUNT(some column)
else '0'
end
but its not working.
can anyone tell me how to count only particular rows which satisfy the given condition in WHEN Clause.
i m trying to add many columns from different tables by joining them and then adding column one by one satisfying different conditions.
my final table looks like this.
items in stock-----|-----items_not_in_STock----|----Items_fully_in_stock
its something like this...!!!
secondly , what if i dont want any ELSE condition.
is it possible???

I am not quite sure I understood your "requirement"...no tables, no sample data, no expected output...
can anyone tell me how to count only particular rows which satisfy the given condition in WHEN ClauseAssuming your requirement is above i.e. getting count of resultset based on different conditions, following might help you.
SQL> select sum(case when object_id > 10000 then 1 end) from t ;
SUM(CASEWHENOBJECT_ID>10000THEN1END)
                                  74

Similar Messages

  • Need help on case statements to validate records

    Hi Experts ,
    My table :
    seq_num
    col2
    col3
    col4
    1
    A
    12345
    P
    2
    B
    1
    123%23
    3
    C
    1
    23AB
    4
    D
    1
    20131001
    5
    E
    1
    6
    A
    13245
    Q
    7
    B
    1
    12345
    8
    C
    2
    1234*AB
    9
    D
    5
    20140112
    10
    E
    1
    00020
    my output
    seq_num
    col2
    col3
    col4
    Status
    Reason
    1
    A
    12345
    P
    Valid
    2
    B
    1
    123%23
    invalid
    Special Character for col4
    3
    C
    1
    23AB
    Valid
    4
    D
    1
    20131001
    Valid
    5
    E
    1
    invalid
    null for col4
    6
    A
    13245
    Q
    invalid
    Invalid character col4 || invalid number for col3
    7
    B
    1
    12345
    Valid
    8
    C
    2
    1234*AB
    Invalid
    Special Character col4 ||invalid col3
    9
    D
    5
    20140112
    invalid
    Future dates col4 ||invalid col3
    10
    E
    1
    00020
    Valid
    Sql :
    with t as
    ( select 1 as seq_num,'A' as col2 ,12345 as col3 ,'P' as col4 from dual
    union all
    select 2 ,'B',1,'123%23' from dual
    union all
    select 3,'C',1,'23AB' from dual
    union all
    select 4,'D',1,'21-02-2013' from dual
    union all
    select 5,'E',1,null from dual
    union all
    select 6,'A,13245,'Q' from dual
    union all
    select 7,'B',1,12345 from dual
    union all
    select 8,'C',2,'1234*AB' from dual
    union all
    select 9,'D',5,'25-01-2014' from dual
    union all
    select 10,'E',1,20 from dual
    I am applying rules on col3 and col4 for each records row-wise.
    I need case statements to populate status and reason columns after applying below rules
    Rules
    Col3 :
    For A record ,it should be 12345 always .
    For B,C,D,E , record should be always 1
    col4
    For A record , it should be either P or R
    No null values for all A, B,C,D,E records
    for B record , it dont contain special charecters
    for C RECORD ,  it dont contain special charecters
    for D record ,it should not contain future dates (dates are in yyyymmdd format and  less than  sysdates are valid )
    I have other columns as well ,as i not included here
    .It would be great if you Could  help on case statements
    Thanks and Regards,
    Sumanth

    I've adjusted Gregs nice example a bit. This should work:
    with w_base as (
          select seq_num, col2, col3, col4,
                 case when (col2 = 'A'                 AND col3 = 12345 )
                        OR (col2 in ('B','C','D','E')  AND col3 = 1)
                            then '' else '||invalid col3' end ||
                 case when (col2 = 'A'        AND col4 not IN ( 'P', 'R' ) )
                            then '||invalid col4' else '' end ||
                 case when (col2 IN ( 'B', 'C' )   AND col4 != translate(col4, 'a!@#$%^*()','a') )
                            then '||special character for col4' else '' end ||
                 case when (col2 = 'D'        AND col4 >= to_char(sysdate,'yyyymmdd') )
                            then '||future dates col4' else '' end
                   reason
          from ( select 1 as seq_num, 'A' as col2, 12345 as col3, 'P' as col4  from dual union all
                 select 2,            'B',         1,             '123%23'     from dual union all
                 select 3,            'C',         1,             '23AB'       from dual union all
                 select 4,            'D',         1,             '20130212'   from dual union all
                 select 5,            'E',         1,             null         from dual union all
                 select 6,            'A',         13245,         'Q'          from dual union all
                 select 7,            'B',         1,             '12345'      from dual union all
                 select 8,            'C',         2,             '1234*AB'    from dual union all
                 select 9,            'D',         5,             '20140125'   from dual union all
                 select 10,           'E',         1,             '20'         from dual )
    Select seq_num, col2, col3, col4,
           case when reason is null then 'Valid' else 'Invalid' end status,
           substr(reason, 3 ) reason
    from w_base
    It returns
    SEQ_NUM
    COL2
    COL3
    COL4
    STATUS
    REASON
    1
    A
    12345
    P
    Valid
    2
    B
    1
    123%23
    Invalid
    special character for col4
    3
    C
    1
    23AB
    Valid
    4
    D
    1
    20130212
    Valid
    5
    E
    1
    Valid
    6
    A
    13245
    Q
    Invalid
    invalid col3||invalid col4
    7
    B
    1
    12345
    Valid
    8
    C
    2
    1234*AB
    Invalid
    invalid col3||special character for col4
    9
    D
    5
    20140125
    Invalid
    invalid col3||future dates col4
    10
    E
    1
    20
    Valid
    edited some bugs :) now it should be fine! really

  • Regarding case statement and decode function

    Hi Experts,
    I have question.....regarding case statement and decode statement....
    Can you please explain me that which one will be efficient,to place in insert statement...
    insert statement(
    (case when ........then
                         case when ....then
                         else
                         end)
      else
    end)
    or
    insert statement(
    case when.....then
    decode(....)
    else
    end)
    Can you people explain me which one is more efficient method?
    Thanks in advance.......

    The are major differences to talk about in case of CASE vs DECODE, but performance wise both are pretty much the same.
    Have a look at Tom's thread
    Ask Tom "better performance - case or decode"
    I would suggest to use CASE whenever possible. Don't worry about the performance part.

  • Just a FYI regarding Case statements in ORacle 8i in pl/sql

    Well..I saw numerous posts in this forum regarding not being able to do case statement within pl/sql. Well..you can do
    that using dynamic SQL. IT works like a champ.

    Hi,
    Try first to (re)compile ll invalid objects. then
    you can run:
    select owner,type,count(*) from all_errors
    group by owner,typeif you still have uncompiled objects, then report the different errors:
    select * from all_errors...

  • Regarding case statement

    Hi guys,
    i am getting a very strange problem. i have score column. basis of this column i am sorting the dataand after it i am giving rank to it.
    but for one employee it is not giving rank. giving null.so please any suggestion.
    i am applying case statement for it.

    It can be because of case statement...Whats the purpose of case statement here ? plz elaborate..
    Try removing case and check if you are getting same error

  • Need help regarding SELECT statement

    Hello, first time here but need help badly.
    I been using SQL syntax with another SQL server by the following statement doesnt seem to work in Oracle database.
    SELECT firstname+" "+lastname AS fullname FROM customers
    basicially, I just want to display date from two column as one column.
    Thanks

    Oracle has pipe sign for concate
    SELECT firstname||' '||lastname AS fullname
    FROM customers;Khurram

  • Need Help on Case Statement

    Hi all
    I'm stuck on scenario. I have query something like this
    Select  HIRE_DATE,
                ENAME,
            ( case when EMP_NO=10 then
              case when exist
                   select ENAME from
                    EMP_DTL
                   where EMP_DTL.EMP_NO=EMP_MST.EMP_NO
              ) end case
             case when EMP_NO in (10,20,30,40) then
               case when exist
              select ENAME from
                    EMP_DTL
                   where EMP_DTL.EMP_NO=EMP_MST.EMP_NO)
              As TARGET_CODE)
    FROM EMP_MST;
    now I'm trying to return ENAME (TARGET_CODE)  that I use in my Case but I'm getting error. I don't know why. Please guide me.
    Regards
    Shu

    I don't understand your query at all, so I'm just taking a guess here:
    SQL> select m.emp_no
      2        ,m.hire_date
      3        ,m.e_name
      4        ,d.pl_code
      5  from   emp_mst m
      6  left   join
      7              (select emp_no, pl_code
      8               from   emp_dtl
      9               where  (
    10                          (emp_no = 10 and pl_code = 'BB')
    11                       or (emp_no in (20,30) and pl_code = 'LOB')
    12                      )
    13              ) d
    14              on (d.emp_no = m.emp_no)
    15  ;
    EMP_NO HIRE_DATE    E_NAME   PL_CODE
        10 12-JAN-2013  ROB      BB
        20 13-JAN-2013  ROBERT   LOB
        30 14-JAN-2013  JILL
    EDIT: changed the query.

  • Need Help:Regarding Case when we should receive an email if we haven't receive feed within SLA.

    Hello Cisco Team/Everyone,
    We are facing one major hurdle we need to configure a case when a email should be sent to business if the feed is not received within SLA.
    At present what we can configure is if feed is being received within SLA then an email will be sent informing about the timely delievery of feed and if we receive feed after SLA then also an email will be sent to business informing that feed received late but if we haven't received the feed within SLA then please let us know how to configure Email Action ? ( informing that we haven't received the feed within SLA )
    Thanks.

    You have muliple solutions so we probably need more details
    At a mininum you would want 2 different email events with different triggers and 2 different email actions
    You may or may not have 2 different jobs
    How we decided to handle:
    We decided to create and ALERT Job Group for our SLA notifications/alerts
       (you can make it its own parent or integreate with another Job Group depeending on what makes sense to you)
    We organized the next level by time windows in military time
    Alert\
      \0000_0259 <-- alerts that trigger between midnight and 3am (they line up top to bottom that way and visually progress)
    If this is a file based alert you create a dependecy on the file not existing
    Next put a Time Window on it lets say start at 12:15am and end at 1:00 am
    Here's the counter intuitive part:
    You create an event to send an email to business on job completed normally that the SLA was breached
              if the file didn't exist by 1am then this executes and completes normally
              We use powershell to write-output of text we want to send for that particular business unit
              We use in email action
    You create an event to skip the job if job not ready by the end of its time window
              if the file existed then the depdendcy is not statisfied and job essentailly is not ready by end of time window 1am
              Operations teams consider "skipped" a "OK" status since we do it on purpose
    Other options
    You could also use exit codes to do similar things, if your process that is evaluating the SLA is produce different exit codes, Then you can apply different events to the exit codes
    Hope that helps!
    Marc

  • Need help in CASE statement

    HI ,
    My query gives output like this
    Cno BO DT countkey
    RF036     I     1          156
    RF036     I     8          445
    RF036     O     3          1320
    RF036     O     D          2547
    I need output like
    Cno DM CC MC BC
    RF036 156 445 1320 2547
    Thanks,
    Lony

    Based on your data...
    WITH D AS (
       SELECT 'RF036' AS CNO, 'I' AS BO, '1' AS DT, 156  AS COUNTKEY FROM DUAL
       UNION ALL
       SELECT 'RF036'       , 'I'      , '8'      , 445              FROM DUAL
       UNION ALL
       SELECT 'RF036'       , 'O'      , '3'      , 1320             FROM DUAL
       UNION ALL   
       SELECT 'RF036'       , 'O'      , 'D'      , 2547             FROM DUAL  
    SELECT CNO, MAX(DM) AS DM, MAX(CC) AS CC, MAX(MC) AS MC, MAX(BC) AS BC
    FROM (
       SELECT CNO, CASE WHEN BO = 'I' AND DT = '1' THEN COUNTKEY ELSE NULL END DM,
                   CASE WHEN BO = 'I' AND DT = '8' THEN COUNTKEY ELSE NULL END CC,
                   CASE WHEN BO = 'O' AND DT = '3' THEN COUNTKEY ELSE NULL END MC,
                   CASE WHEN BO = 'O' AND DT = 'D' THEN COUNTKEY ELSE NULL END BC
       FROM D
    GROUP BY CNO

  • Help with case statement

    Can any one whats wrong in below sql....Error is missing expression ...?
    SELECT CASE WHEN A.STG_RECEIVER_MODEL_COUNT = 0 THEN 'STAGING RECEIVER MODEL CAPABILITY CONSISTS OF 0 RECORDS'
    WHEN A.STG_RECEIVER_MODEL_CAP > 0 THEN
    CASE WHEN ( B.ERROR_COUNT_FOR_4 = 90 * ( A.STG_RECEIEVER_MODEL_CAP_COUNT / 100) ) THEN 'MORE WARNING ERRORS FOR STG_PACKAGE'
    ELSE 'WARNING ERRORS ARE LESS THAN 90 PERCENT'
    END CASE
    END AS WARNING_COUNT
    FROM
    SELECT COUNT(*) AS STG_RECEIVER_MODEL_COUNT FROM STG_RECEIVER_MODEL_CAPABILITY
    ) A,
    SELECT count(*)AS ERROR_COUNT_FOR_4 FROM STG_ERROR_DETAIL
    WHERE ERROR_CAT_ID = 4
    AND MAP_NAME = 'STG_RECEIVER_MODEL_CAPABILITY'
    AND CREATE_DT = (SELECT CREATE_DATE FROM QA_CREATE_DATE)
    ) B
    ;

    If you rewrite properly yourquery, it could be easier to find out the error :<br>
    SELECT CASE WHEN A.STG_RECEIVER_MODEL_COUNT = 0
                THEN 'STAGING RECEIVER MODEL CAPABILITY CONSISTS OF 0 RECORDS'
                WHEN A.STG_RECEIVER_MODEL_CAP > 0
                THEN CASE WHEN ( B.ERROR_COUNT_FOR_4 = 90 * ( A.STG_RECEIEVER_MODEL_CAP_COUNT / 100) )
                          THEN 'MORE WARNING ERRORS FOR STG_PACKAGE'
                          ELSE 'WARNING ERRORS ARE LESS THAN 90 PERCENT'
                     END <s>CASE</s>
           END AS WARNING_COUNT
    FROM (SELECT COUNT(*) AS STG_RECEIVER_MODEL_COUNT FROM STG_RECEIVER_MODEL_CAPABILITY) A,
         (SELECT count(*)AS ERROR_COUNT_FOR_4
          FROM STG_ERROR_DETAIL
          WHERE ERROR_CAT_ID = 4
          AND MAP_NAME = 'STG_RECEIVER_MODEL_CAPABILITY'
          AND CREATE_DT = (SELECT CREATE_DATE FROM QA_CREATE_DATE)) B<br>
    <br>
    Nicolas.

  • Query regarding case statement

    Hi,
    I have a SP which has in_rc as an input parameter
    Now I have a select query which is as follows in pseudo
    select....
    where <condition1>
    And <condition2>
    And <condition3>
    Now my requirement is that I want to add one more filter based on the in_rc
    ie if in_rc is not null, then I want to add one more filter condition along with the existing condition else only the existing condition.
    pseudo:
    select....
    where <condition1>
    And <condition2>
    And <condition3>
    case <in_rc><when not null>then
    AND <condition 4>
    possible?
    regds,
    Sun

    I am not pedantic. Just pointing out some things to be aware of (should be allowed, shouldn't it?);)
    It only requires a few more NVL's. ;) or lnnvl? ;)
    michaels>  VAR mgr number
    michaels>  EXEC :mgr := 7782
    michaels>  SELECT ename, mgr
      FROM emp
    WHERE lnnvl (mgr != :mgr)
    ENAME             MGR
    KING                     /*  want nulls or not?  */
    MILLER           7782
    michaels>  EXEC :mgr := NULL
    michaels>  SELECT ename, mgr
      FROM emp
    WHERE lnnvl (mgr != :mgr)
    ENAME             MGR
    SMITH            7902
    ALLEN            7698
    WARD             7698
    JONES            7839
    MARTIN           7698
    BLAKE            7839
    CLARK            7839
    SCOTT            7566
    KING                
    TURNER           7698
    ADAMS            7788
    JAMES            7698
    FORD             7566
    MILLER           7782

  • 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

  • Help: How to use Case statement in Interface ODI11g?

    Hi
    From my Source base i am getting 'Year' Values and i want translate these values into a code in interface and after translate want to push to Target system.
    Example:
    From source Database i am getting value for
    Year
    2010
    2011
    2012
    When i get Year 2010 i want to modify value into 'FY10'
    when i get Year 2011 i want to modify value into 'FY11'
    and same for Year 2012 TO 'FY12'
    I was trying to do by Case statement but didn't got success.
    I dont want to create Lookup table in source system.
    Any help in this regards.
    Thanks
    Regards
    Sher
    Edited by: Sher Ullah Baig on Aug 26, 2012 5:52 PM

    CASE
    WHEN source_column='2010' THEN 'FY10'
    WHEN source_column='2011' THEN 'FY11'
    WHEN source_column='2012' THEN 'FY12'
    END

  • Please help: Case Statement - ORA-01722: invalid number Error

    Hi All,
    I'm trying to use Case statement to recode Cancelled statuses into two groups
    'Reject' if the difference between first date and last date is less than 29 and
    'Accept' if the difference between first date and last date is greater than 30
    Here is my 'test' data:
    STATUS     FIRST_DATE     LAST_DATE
    Transfer     10/08/2011     10/09/2011
    Mover     10/08/2011     15/09/2011
    Cancel     10/08/2011     16/09/2011
    Cancel     10/08/2011     5/09/2011
    Here is the syntax
    select a.*,
    (CASE WHEN a.STATUS ='Cancel' THEN (round(a.LAST_DATE-a.FIRST_DATE))
    ELSE 0
    END) CAN_DAYS,
    (CASE WHEN 'CAN_DAYS' >29 THEN 'Reject'
    WHEN 'CAN_DAYS' <30 THEN 'Accept'
    END) Reject_Accept
    from test a
    The first CASE statement works fine and gives me this
    STATUS     FIRST_DATE     LAST_DATE     CAN_DAYS
    Transfer     10/08/2011     10/09/2011     0
    Mover     10/08/2011     15/09/2011     0
    Cancel     10/08/2011     16/09/2011     37
    Cancel     10/08/2011     5/09/2011     26
    but the second CASE produces ORA-01722: invalid number Error. It is something to do with the CAN_DAYS data type, Oracle doesn't see it as Numeric i think.
    The result i want to see would be
    STATUS     FIRST_DATE     LAST_DATE     CAN_DAYS Reject_Accept
    Transfer     10/08/2011     10/09/2011     0
    Mover     10/08/2011     15/09/2011     0
    Cancel     10/08/2011     16/09/2011     37 Accept
    Cancel     10/08/2011     5/09/2011     26 Reject
    Could someone please help me to fix the syntax?
    Thank you in advance,

    This seems to give you your desired results...
    with test as
      ( select 'Transfer' as status, to_date('10/08/2011') as first_date, to_date('10/09/2011') as last_date  from dual union all
        select 'Mover'             , to_date('10/08/2011')              , to_date('15/09/2011')               from dual union all
        select 'Cancel'            , to_date('10/08/2011')              , to_date('16/09/2011')               from dual union all
        select 'Cancel'            , to_date('10/08/2011')              , to_date('05/09/2011')               from dual
    select  status
         ,  first_date
         ,  last_date
         ,  CAN_DAYS
         ,  CASE
              WHEN CAN_DAYS =   0 THEN NULL
              WHEN CAN_DAYS <  30 THEN 'Reject'
              WHEN CAN_DAYS >= 30 THEN 'Accept'
            END  Reject_Accept
      from  ( select  status
                   ,  first_date
                   ,  last_date
                   ,  CASE
                        WHEN STATUS ='Cancel' THEN round(LAST_DATE-FIRST_DATE)
                        ELSE 0
                      END  CAN_DAYS
                from  test
    ;

  • Need help in this sql query to use Case Statement

    hi All,
    I have the below query -
    SELECT DISTINCT OFFC.PROV_ID
    ,OFFC.WK_DAY
    ,CASE
    WHEN OFFC.WK_DAY ='MONDAY' THEN 1
    WHEN OFFC.WK_DAY ='TUESDAY' THEN 2
    WHEN OFFC.WK_DAY ='WEDNESDAY' THEN 3
    WHEN OFFC.WK_DAY ='THURSDAY' THEN 4
    WHEN OFFC.WK_DAY ='FRIDAY' THEN 5
    WHEN OFFC.WK_DAY ='SATURDAY' THEN 6
    WHEN OFFC.WK_DAY ='SUNDAY' THEN 7
    END AS DOW
    ,OFFC.OFFC_OPENG_TIME
    ,OFFC.OFFC_CLSNG_TIME
    FROM GGDD.PROV_OFFC_HR OFFC
    WHERE OFFC.PROV_ID='0000600'
    WITH UR;
    this query is bringing results in 6 differnt rows with opening and closing time for each day separately. I want to generate the data in one row with each day having opening and closing time, so for 7 days, total 14 columns with opening and closing time. But i am not able to do that using case statement.
    can somebody help me in achieving that.
    thanks,
    iamhere

    Hi,
    Welcome to the forum!
    That's called a Pivot .
    Instead of having 1CASE expression, have 14, one for the opening and one for the closing time each day, and do GROUP BY to combine them onto one row.
    SELECT       OFFC.PROV_ID
    ,       MIN (CASE WHEN OFFC.WK_DAY ='MONDAY'    THEN OFFC.OFFC_OPENG_TIME END)     AS mon_opn
    ,       MIN (CASE WHEN OFFC.WK_DAY ='MONDAY'    THEN OFFC.OFFC_CLSNG_TIME END)     AS mon_cls
    ,       MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY'   THEN OFFC.OFFC_OPENG_TIME END)     AS tue_opn
    ,       MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY'   THEN OFFC.OFFC_CLSNG_TIME END)     AS tue_cls
    FROM        GGDD.PROV_OFFC_HR OFFC
    WHERE       OFFC.PROV_ID     = '0000600'
    GROUP BY  offc.prov_id
    ;This assumes there is (at most) only one row in the table for each distinct prov_id and weekday. If not, what do you want to do? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    The staement above works in Oracle 8.1 and up, but there's a better way (SELECT ... PIVOT) available in Oracle 11. What version are you using? (It's always a good idea to include this when you post a question.)
    Edited by: Frank Kulash on Jan 6, 2011 8:22 PM

Maybe you are looking for

  • How to reset numbers in real time display in Cisco Supervisor Desktop?

    Hi all, I wonder about numbers in real time display in Cisco Supervisor Desktop. There are many statistic number for example : Call Handled, Max Talking, Avg Talking, Max Ready, Total Ready, Call abandoned, etc. Is there any way to reset those number

  • Need API or interface table for manufacturer & mfr part

    We are developing integration that would push data to following base tables. 1.     mtl_item_categories 2.     mtl_manufacturers 3.     mtl_mfg_part_numbers We could find interface table for category but nothing for manufacturers and manufacturer par

  • Everytime trying 2 open itunes it says itunes has encountered a problem.

    i keep installed itunes then i went 2 open it and a message came up sayin itunes has encountered a problem and needs 2 close. then it says send error report or dont send error report. i have tryed sending an error report but it still keeps doing it.T

  • Unexpected result from XMLELEMENT() - nothing but XMLTYPE()

    In testing XML DB thru SQL*PLUS under an Oracle 9.2 environment, I issued following query: <QueryStatement> SELECT XMLELEMENT("Name", p.name) as "Result" FROM PROD_PERIODS p </QueryStatement> But it returned nothing but following message that contain

  • XML to XSL

    I am strugling to output my xml document in an xsl format. My xml document is created in a function, and I have another function that takes the xml and transform it to an xsl document... something is wrong somewhere the output to xsl is not happening