Question in Case Statement....

Hi Gurus, I have a case statement as below: Here I have to get the Initials of person only where desc = underwriter. ( P.S sometimes there are multiple underwriter, thats why i put the min() around a character.)
Initials =
min(case when ( table1.DESC ) = 'UNDERWRITER' then table2.INITIALS when ( table1.DESC ) ='ASSIGN' then table2.INITIALS end )
Please see data below:
Underwriter Initials : SE
-------------------------------XY
Assign Initials : NR
----------------------------PQ
Through my SQL I intend to get SE for Underwriter. But my SQL returns NR as it it is the minimum in the order. Any ideas please...........
P.S: The Initials column is actually displayed in one of my reports; and the Initials column is dynamic; meaning ....i am prompting the User to select DESC values: Underwriter or Assign
and based on that the prompt; the Initials Column should display either Underwriter Initials or Assign Initials.
Thanks a lot.

Hi Frank, thanks a lot for your reply. I am a lot closer to solving it now. A little more help please.
The expected output is like below:
1) Initials 2) Desc
SE-------------- Underwriter
NR---------------Assign
Yes , i understand the whole purpose of case is to give different scnarios but in this case i have tried using something like:
case when CR_ROLE_TYPE_DESC_REF.ROLE_TYP_DESC = :p_role_typ_desc then CR_PARTY_DIM.PARTY_NAME_INITIALS end
but it gives me the wrong values. which i think is due to the Decode statement below:
Please see the modified SQL:
Here the user chooses prompts (DESC: Underwriter) and also pick a person name which has a decode for ASSIGN in it
SELECT
CR_ACCOUNT_DIM.ACCOUNT_NM,
min(case when ( CR_ROLE_TYPE_DESC_REF.ROLE_TYP_DESC ) = 'UNDERWRITER' then CR_PARTY_DIM.PARTY_NAME_INITIALS when ( CR_ROLE_TYPE_DESC_REF.ROLE_TYP_DESC ) ='ASSIGN' then CR_PARTY_DIM.PARTY_NAME_INITIALS end)
FROM
CR_ACCOUNT_DIM,
CR_CNSMR_RQST_PARTY_ROLE_RLSHP,
CR_PARTY_DIM,
CR_CONSUMER_RQST_DIM,
CR_BUSINESS_SGMNT_REF,
CR_CONSUMER_RQST_FACT
WHERE
( CR_CONSUMER_RQST_FACT.CNSMR_RQST_DIM_PK_ID=CR_CONSUMER_RQST_DIM.CNSMR_RQST_DIM_PK_ID )
AND ( CR_PARTY_DIM.PARTY_DIM_PK_ID= CR_CNSMR_RQST_PARTY_ROLE_RLSHP.PARTY_DIM_PK_ID )
and
decode(CR_CNSMR_RQST_PARTY_ROLE_RLSHP.ROLE_TYP_DESC,'ASSIGN',CR_PARTY_DIM.FIRST_NM || ' ' || CR_PARTY_DIM.MIDDLE_INTL || ' ' || CR_PARTY_DIM.LAST_NM ) = 'ROBERTO NIEL'
Any ideas please....

Similar Messages

  • Question about case statement

    It looks like for the case statement in Linux shell script, it the first condition meet, it won't execute the next anymore, it would exit.
    so, for the code like the following, I wonder if there is anyway to output there lines if the $NUM is 0:
    case $NUM in
         1 | 0) echo "one or zero" ;;
         2 | 0) echo "two or zero" ;;
         3 | 0) echo "three or zero" ;;
         *) echo "INVALID NUMBER!" ;;
    esacexpect output:
    one or zero
    two or zero
    three or zerocurrent output:
    one or zero

    Every case statement must be terminated with ";;"
    http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html
    What's wrong with using functions as I outlined earlier? It safes you typing repeated commands, if that is what you are trying to accomplish. Alternatively you could also construct a case statement using an if-then clause. Unfortunately Unix shell does not have a "goto" statement, which would make it sometimes easier to control work-flow.
    For instance:
    $ ./test 1
    0 or 1
    $ ./test 2
    0 or 2
    $ ./test 3
    0 or 3
    $ ./test 0
    0 or 1
    0 or 2
    0 or 3
    $ cat ./test
    NUM=$1
    if [ $NUM = 1 ] || [ $NUM = 0 ]; then
       echo "0 or 1"
    fi
    if [ $NUM = 2 ] || [ $NUM = 0 ]; then
       echo "0 or 2"
    fi
    if [ $NUM = 3 ] || [ $NUM = 0 ]; then
       echo "0 or 3"
    fiEdited by: Dude on Jan 27, 2011 1:37 PM

  • Question about this case statement...Never seen it written this way

    I have this case statement written by a developer and i am trying to understand what he was trying to accomplish with writing it in this manner
    CASE t_CELL
                    WHEN 'XML' THEN
                        'Mods'
                    ELSE
                        CASE WHEN t_CELL IS NOT NULL THEN
                            t_Cell
                        ELSE
                            CELL
                        END
                    END
                = CELL OR CASE t_CELL
                    WHEN 'XML' THEN
                        'Amps'
                    ELSE
                        CASE WHEN t_CELL IS NOT NULL THEN
                            t_Cell
                        ELSE
                            CELL
                        END
                    END = CELL);What is throwing me off is the = CELL that was included..Kindly note, this is just a sample code

    Hi,
    If x and cell are strings, then I trust you understand what it means to compare them by saying:
    x = cellx can be any kind of expression that returns a string.
    For example, do you understand what it means if x is a string literal, like this?
    'Amps' = cellDo you understand what it means when x is another string column, like this?
    t_cell = cellDo you understand what it means when x is a function that returns a string, like this?
    TO_CHAR (SYSDATE, 'Dy') = cellIn the code you posted, the CASE expression is just another way of specifying a string value for x:
    CASE ... END = cellA CASE expression is just another kind of expression, that is, another thing that can replace x on the left of the = sign.
    It looks like the condition you posted is equivalent to:
    (     (     t_cell     = 'XML'
         AND     cell     IN ('Amps', 'Mods')
    OR     (     t_cell     != 'XML'
         AND     cell     = t_cell
    OR          t_cell     IS NULL
    )

  • Questions on Room State

    Hey guys,
         I got some questions on room state:
         1) Does the room state actually take some effect or just an indicating flag? For example, if a room state is ROOM_STATE_ENDED, will it prevent any incoming user connection?
         2) I tried to login as a guest, the room setting is HostNotArrived. Then I logged in as an owner. The state was switched to Active. The result seems fine until now. However, when I quit the owner app by closing the brower tab, and checked the quest page, the room state was still in Active, rather than switch back to  HostNotArrived or any other state. Is it an expected result? How can I let the room state update to date if the owner quit in this way?
         3) For my scenario, I'd like to build a room only avaiable when owner arrived. That is, if a guest arrived before the owner arrived or after the owner left, he will not be allowed to do anything , even the connection should be rejected. Is that possible?
         Thanks a lot!
    Jinni

    Hi Jinni,
      1) It should prevent any incoming connection other than OWNERs of the room.
      2) HostNotArrived happens only before a room has been "kicked off" by the owner, and not afterwards (even if the owner has left the room, it's still fair to say he had "arrived").
      3) I'm not sure there's a perfect way to do this yet. We'll be looking into this use case, which feels like you need some "server-to-server" integration : the ability to detect an owner has left, then a way to shut down the room. Does that seem right to you? For now, a quick way of achieving the functionality (if not the security) is to have your clients detect that there's no owner, and disconnect.
       hope that helps
       nigel

  • More than one variable at a time in case statement

    hi i am using oracle 10g database.
    can it be possible ?
         in if condition we can have more than one variable in one condition
           if a=100 then
               b:=200;
               c:=300;
           end if;
    how it will be in case statement ? i have searched lot on internet but didnt get
    case
    when a=100 then 200, 300 ... ???
    thanks

    Hi FRNzzz!! wrote:
    hi i am using oracle 10g database.
    can it be possible ?
    in if condition we can have more than one variable in one condition
    if a=100 then
    b:=200;
    c:=300;
    end if; how it will be in case statement ? i have searched lot on internet but didnt get
    case
    when a=100 then 200, 300 ... ???
    thanksnot sure if I got your question correctly, is this what you are looking for?
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2      a number := 100;
      3      b number;
      4      c number;
      5  begin
      6      case
      7        when a = 100 then
      8          b := 200;
      9          c := 300;
    10        when a = 0 then
    11          b := 300;
    12          c := 400;
    13        else
    14          b := 0;
    15          c := 0;
    16      end case;
    17  dbms_output.put_line('a :'||a||' b :'||b||' c :'||c);
    18* end;
    SQL> /
    a :100 b :200 c :300
    PL/SQL procedure successfully completed.

  • 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

  • Case statement and Decode function both are not working in Select cursor.

    I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. On the other hand both the things work in just select statement.
    See the first column in select (PAR_FLAG), I need to have this evaluated along with other fields. Can you please suggest some thing to make this work. And also I would like to
    know the reason why decode is not working, I heard some where Case statement do not work with 8i.
    Author : Amit Juneja
    Date : 06/20/2011
    Description:
    Updates the Diamond MEMBER_MASTER table with the values from
    INC.MEM_NJ_HN_MEMBER_XREF table.
    declare
    rec_cnt number(12) := 0;
    commit_cnt number(4) := 0;
    cursor select_cur is
    Select DECODE(1,
    (Select 1
    from hsd_prov_contract R
    where R.seq_prov_id = PM.seq_prov_id
    and R.line_of_business = H.line_of_business
    and R.PCP_FLAG = 'Y'
    and R.participation_flag = 'P'
    and SYSDATE between R.EFFECTIVE_DATE AND
    NVL(R.TERM_DATE,
    TO_DATE('31-DEC-9999', 'DD-MON-YYYY'))),
    'Y',
    'N') PAR_FLAG,
    H.SEQ_ELIG_HIST,
    H.SEQ_MEMB_ID,
    H.SEQ_SUBS_ID,
    H.SUBSCRIBER_ID,
    H.PERSON_NUMBER,
    H.EFFECTIVE_DATE,
    H.TERM_DATE,
    H.TERM_REASON,
    H.RELATIONSHIP_CODE,
    H.SEQ_GROUP_ID,
    H.PLAN_CODE,
    H.LINE_OF_BUSINESS,
    H.RIDER_CODE_1,
    H.RIDER_CODE_2,
    H.RIDER_CODE_3,
    H.RIDER_CODE_4,
    H.RIDER_CODE_5,
    H.RIDER_CODE_6,
    H.RIDER_CODE_7,
    H.RIDER_CODE_8,
    H.MEDICARE_STATUS_FLG,
    H.OTHER_STATUS_FLAG,
    H.HIRE_DATE,
    H.ELIG_STATUS,
    H.PREM_OVERRIDE_STEP,
    H.PREM_OVERRIDE_AMT,
    H.PREM_OVERRIDE_CODE,
    H.SEQ_PROV_ID,
    H.IPA_ID,
    H.PANEL_ID,
    H.SEQ_PROV_2_ID,
    H.SECURITY_CODE,
    H.INSERT_DATETIME,
    H.INSERT_USER,
    H.INSERT_PROCESS,
    H.UPDATE_DATETIME,
    H.UPDATE_USER,
    H.UPDATE_PROCESS,
    H.USER_DEFINED_1,
    H.SALARY,
    H.PEC_END_DATE,
    H.REASON_CODE,
    H.PEC_WAIVED,
    H.BILL_EFFECTIVE_FROM_DATE,
    H.BILLED_THRU_DATE,
    H.PAID_THRU_DATE,
    H.SUBSC_DEPT,
    H.SUBSC_LOCATION,
    H.USE_EFT_FLG,
    H.BENEFIT_START_DATE,
    H.SEQ_ENROLLMENT_RULE,
    H.MCARE_RISK_ACCRETION_DATE,
    H.MCARE_RISK_DELETION_DATE,
    H.MCARE_RISK_REFUSED_DATE,
    H.COMMENTS,
    H.USER_DEFINED_2,
    H.USER_DEFINED_3,
    H.RATE_TYPE,
    H.PCPAA_OCCURRED,
    H.PRIVACY_ON,
    H.PCP_CHANGE_REASON,
    H.SITE_CODE,
    H.SEQ_SITE_ADDRESS_ID,
    PM.seq_prov_id rendered_prov
    from hsd_member_elig_history H,
    INC.PCP_REASSIGN_RPRT_DATA P,
    hsd_prov_master PM
    where P.subscriber_id = H.subscriber_id
    and P.rendered_pcp = PM.provider_ID
    and H.elig_status = 'Y'
    and (H.term_date is NULL or H.term_date >= last_day(sysdate))
    order by H.Seq_memb_id;
    begin
    for C in select_cur loop
    rec_cnt := rec_cnt + 1;
    update hsd_member_elig_history
    set term_date = TRUNC(SYSDATE - 1),
    term_reason = 'PCPTR',
    update_datetime = SYSDATE,
    update_user = USER,
    update_process = 'TD33615'
    where seq_elig_hist = C.seq_elig_hist
    and seq_memb_id = C.seq_memb_id;
    INSERT INTO HSD_MEMBER_ELIG_HISTORY
    (SEQ_ELIG_HIST,
    SEQ_MEMB_ID,
    SEQ_SUBS_ID,
    SUBSCRIBER_ID,
    PERSON_NUMBER,
    EFFECTIVE_DATE,
    TERM_DATE,
    TERM_REASON,
    RELATIONSHIP_CODE,
    SEQ_GROUP_ID,
    PLAN_CODE,
    LINE_OF_BUSINESS,
    RIDER_CODE_1,
    RIDER_CODE_2,
    RIDER_CODE_3,
    RIDER_CODE_4,
    RIDER_CODE_5,
    RIDER_CODE_6,
    RIDER_CODE_7,
    RIDER_CODE_8,
    MEDICARE_STATUS_FLG,
    OTHER_STATUS_FLAG,
    HIRE_DATE,
    ELIG_STATUS,
    PREM_OVERRIDE_STEP,
    PREM_OVERRIDE_AMT,
    PREM_OVERRIDE_CODE,
    SEQ_PROV_ID,
    IPA_ID,
    PANEL_ID,
    SEQ_PROV_2_ID,
    SECURITY_CODE,
    INSERT_DATETIME,
    INSERT_USER,
    INSERT_PROCESS,
    UPDATE_DATETIME,
    UPDATE_USER,
    UPDATE_PROCESS,
    USER_DEFINED_1,
    SALARY,
    PEC_END_DATE,
    REASON_CODE,
    PEC_WAIVED,
    BILL_EFFECTIVE_FROM_DATE,
    BILLED_THRU_DATE,
    PAID_THRU_DATE,
    SUBSC_DEPT,
    SUBSC_LOCATION,
    USE_EFT_FLG,
    BENEFIT_START_DATE,
    SEQ_ENROLLMENT_RULE,
    MCARE_RISK_ACCRETION_DATE,
    MCARE_RISK_DELETION_DATE,
    MCARE_RISK_REFUSED_DATE,
    COMMENTS,
    USER_DEFINED_2,
    USER_DEFINED_3,
    RATE_TYPE,
    PCPAA_OCCURRED,
    PRIVACY_ON,
    PCP_CHANGE_REASON,
    SITE_CODE,
    SEQ_SITE_ADDRESS_ID)
    values
    (hsd_seq_elig_hist.nextval,
    C.SEQ_MEMB_ID,
    C.SEQ_SUBS_ID,
    C.SUBSCRIBER_ID,
    C.PERSON_NUMBER,
    trunc(SYSDATE),
    C.TERM_DATE,
    C.TERM_REASON,
    C.RELATIONSHIP_CODE,
    C.SEQ_GROUP_ID,
    C.PLAN_CODE,
    C.LINE_OF_BUSINESS,
    C.RIDER_CODE_1,
    C.RIDER_CODE_2,
    C.RIDER_CODE_3,
    C.RIDER_CODE_4,
    C.RIDER_CODE_5,
    C.RIDER_CODE_6,
    C.RIDER_CODE_7,
    C.RIDER_CODE_8,
    C.MEDICARE_STATUS_FLG,
    C.OTHER_STATUS_FLAG,
    C.HIRE_DATE,
    C.ELIG_STATUS,
    C.PREM_OVERRIDE_STEP,
    C.PREM_OVERRIDE_AMT,
    C.PREM_OVERRIDE_CODE,
    C.SEQ_PROV_ID,
    C.IPA_ID,
    C.PANEL_ID,
    C.SEQ_PROV_2_ID,
    C.SECURITY_CODE,
    SYSDATE,
    USER,
    'TD33615',
    SYSDATE,
    USER,
    'TD33615',
    C.USER_DEFINED_1,
    C.SALARY,
    C.PEC_END_DATE,
    C.REASON_CODE,
    C.PEC_WAIVED,
    C.BILL_EFFECTIVE_FROM_DATE,
    C.BILLED_THRU_DATE,
    C.PAID_THRU_DATE,
    C.SUBSC_DEPT,
    C.SUBSC_LOCATION,
    C.USE_EFT_FLG,
    C.BENEFIT_START_DATE,
    C.SEQ_ENROLLMENT_RULE,
    C.MCARE_RISK_ACCRETION_DATE,
    C.MCARE_RISK_DELETION_DATE,
    C.MCARE_RISK_REFUSED_DATE,
    C.COMMENTS,
    C.USER_DEFINED_2,
    C.USER_DEFINED_3,
    C.RATE_TYPE,
    C.PCPAA_OCCURRED,
    C.PRIVACY_ON,
    C.PCP_CHANGE_REASON,
    C.SITE_CODE,
    C.SEQ_SITE_ADDRESS_ID);
    commit_cnt := commit_cnt + 1;
    if (commit_cnt = 1000) then
    dbms_output.put_line('Committed updates for 1000 records.');
    commit;
    commit_cnt := 0;
    end if;
    end loop;
    commit;
    dbms_output.put_line('Total number of MEMBER_ELIG_HISTROY records inserted : ' ||
    rec_cnt);
    exception
    when others then
    raise_application_error(-20001,
    'An error was encountered - ' || sqlcode ||
    ' -error- ' || sqlerrm);
    end;

    user10305724 wrote:
    I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. Please define what you mean by not working even if your computer screen is near the internet we can't see it.
    You should also look at the FAQ about how to ask a question
    SQL and PL/SQL FAQ
    Particularly *9) Formatting with {noformat}{noformat} Tags* and posting your version.
    know the reason why decode is not working, I heard some where Case statement do not work with 8i.
    Does this mean you are using 8i? Then scalar sub queries - selects within the select list, are not supported, along with CASE in PL/SQL.
    Select DECODE(1,
    * (Select 1
    from hsd_prov_contract R
    where R.seq_prov_id = PM.seq_prov_id
    and R.line_of_business = H.line_of_business
    and R.PCP_FLAG = 'Y'
    and R.participation_flag = 'P'
    and SYSDATE between R.EFFECTIVE_DATE AND
    NVL(R.TERM_DATE,
    TO_DATE('31-DEC-9999', 'DD-MON-YYYY')))*,
    'Y',
    'N') PAR_FLAG,
    >
    exception
    when others then
    raise_application_error(-20001,
    'An error was encountered - ' || sqlcode ||
    ' -error- ' || sqlerrm);
    http://tkyte.blogspot.com/2008/01/why-do-people-do-this.html                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Case Statement in sub query

    Hi, I have two issues, here is my initial code:
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.
    To get around this I did the following which is not very efficient and I'm hoping it can be improved:
    select distinct z.cla_case_no from (
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_MW_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(select distinct z.cla_case_no from (
    select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
    Thanks!!!
    Banner:
    Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    "CORE 11.2.0.2.0 Production"
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production

    Hi,
    It looks like you have another copy of this question:
    Case Statement and sub query
    That's probably not your fault, but you should mark the other copy as "Answered" right away, and then you'll only have to look for replies in one place.
    885178 wrote:
    ... Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.If you know there will only be one, then you can use LAST, and you don't need GrOUP BY
    To get around this I did the following which is not very efficient and I'm hoping it can be improved:
    select distinct z.cla_case_no from (
    select
    cc.name_id_no
    ,cc.discover_date
    ,cc.cla_case_no
    ,max(rl.year_of_incident)Non_MW_Loss_Past_5
    ,rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) zHere's one way:
    SELECT       MIN (cla_case_no) KEEP (DENSE_RANK LAST ORDER BY r1.year_of_incident)
                         AS latest_cla_case_no
    FROM       cla_case     cc
    ,             rbn_loss      rl
    WHERE     cc.name_id_no          = rl.customer_no
    AND       rl.year_of_incident     > TRUNC (cc.discover_date) - 1095
    AND       rl.year_of_incident      < TRUNC (cc.discover_date)
    AND       rl.type_of_loss     < 1000
    AND       rl.timestamp          < TRUNC (cc.discover_date)
    AND       cc.question_class     IN (20, 25)
    ;If you'd post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.
    Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
    SELECT
    ie ,(select distinct z.cla_case_no from (
    select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
    from cla_case cc, rbn_loss rl
    where cc.name_id_no = rl.customer_no
    and rl.year_of_incident < trunc(cc.discover_date)
    and rl.type_of_loss < 1000
    and rl.timestamp < trunc(cc.discover_date)
    and (cc.question_class = 20
    or cc.question_class = 25)
    and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
    --and (trunc(cc.discover_date) <> (rl.year_of_incident))
    group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
    ) z
    where z.cla_case_no = ccx.cla_case_no
    ) Non_MW_Loss_Past_5
    FROM etc
    Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
    What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
    NVL2 (x, 'Y', 'N')returns 'Y' if x is NULL, and it returns 'N' if x is not NULL. X can be a scalar sub-query:
    NVL2 ((SELECT ...), 'Y', 'N')You could also use an EXISTS sub-query:
    CASE
        WHEN  EXISTS (SELECT ...)
        THEN  'Y'
        ELSE  'N'
    END

  • 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 &amp;quot;better performance - case or decode&amp;quot;
    I would suggest to use CASE whenever possible. Don't worry about the performance part.

  • [8i] Case statement generates ORA-00932: inconsistent datatypes

    Note: I am working with an 8i database (yes, it is quite old), and in this situation, I have to deal with the datatypes (i.e. CHAR), I am given to work with.
    I am attempting to calculate the amount of time product waits between manufacturing steps. As I've discovered though, sometimes I get a negative value when subtracting the date the previous step completes from the date the current step starts. As it is generally impossible to start a later step before an earlier step (imagine trying to screw a cap onto a bottle that doesn't have threads cut yet--it just can't happen), what I've found is that sometimes two steps are started on the same day and finished on the same day (though not necessarily the day they started). This situation CAN happen when one person did both steps and logged on to both steps at the same time, rather than logging on to one, then the other. So, what I need to do in these situations is replace the negative number with a zero (I'll treat the later step as having no wait time).
    Here is some sample data:
    (Note: the real data set is the result of a query, and has around 200K rows and more columns, but this should be representative enough to find a solution that works on my actual application.)
    CREATE TABLE     steps
    (     item_id          CHAR(25)
    ,     ord_nbr          CHAR(10)
    ,     sub_nbr          CHAR(3)
    ,     step_nbr     CHAR(4)
    ,     start_date     DATE
    ,     finish_date     DATE
    INSERT INTO steps
    VALUES ('A','0000000001','001','0010',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('A','0000000001','001','0020',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('A','0000000001','001','0030',TO_DATE('01/05/2011','mm/dd/yyyy'),TO_DATE('01/06/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('A','0000000001','002','0010',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('A','0000000001','002','0020',TO_DATE('01/04/2011','mm/dd/yyyy'),TO_DATE('01/04/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('A','0000000001','002','0030',TO_DATE('01/06/2011','mm/dd/yyyy'),TO_DATE('01/07/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('B','0000000002','001','0005',TO_DATE('01/10/2011','mm/dd/yyyy'),TO_DATE('01/12/2011','mm/dd/yyyy'));
    INSERT INTO steps
    VALUES ('B','0000000002','001','0025',TO_DATE('01/10/2011','mm/dd/yyyy'),TO_DATE('01/12/2011','mm/dd/yyyy'));Here is the query I use that returns negative values sometimes:
    SELECT     item_id
    ,     ord_nbr
    ,     sub_nbr
    ,     step_nbr
    ,     start_date - last_step_finished
    FROM     (
         SELECT     s.*
         ,     LAG (s.finish_date)     OVER     (
                                  PARTITION BY     s.item_id
                                  ,          s.ord_nbr
                                  ,          s.sub_nbr
                                  ORDER BY     s.step_nbr
                                  )     AS last_step_finished
         FROM     steps s
    Returns:
    ITEM_ID                   ORD_NBR    SUB STEP START_DATE-LAST_STEP_FINISHED
    A                         0000000001 001 0010
    A                         0000000001 001 0020                        -1.000
    A                         0000000001 001 0030                         3.000
    A                         0000000001 002 0010
    A                         0000000001 002 0020                         2.000
    A                         0000000001 002 0030                         2.000
    B                         0000000002 001 0005
    B                         0000000002 001 0025                        -2.000These are the results I want to see:
    ITEM_ID                   ORD_NBR    SUB STEP START_DATE-LAST_STEP_FINISHED
    A                         0000000001 001 0010
    A                         0000000001 001 0020                         0.000
    A                         0000000001 001 0030                         3.000
    A                         0000000001 002 0010
    A                         0000000001 002 0020                         2.000
    A                         0000000001 002 0030                         2.000
    B                         0000000002 001 0005
    B                         0000000002 001 0025                         0.000And this is what I tried to do to get those results (comment notes what line generated the error):
    SELECT     item_id
    ,     ord_nbr
    ,     sub_nbr
    ,     step_nbr
    ,     CASE
              WHEN     start_dt - last_step_finished     < 0
              THEN     0
              ELSE     start_dt - last_step_finished  -- THIS LINE GENERATES THE ORA-00932 ERROR
         END                         AS days_in_queue
    FROM     (
         SELECT     s.*
         ,     LAG (s.finish_date)     OVER     ( PARTITION BY  s.item_id
                                    ,          s.ord_nbr
                                    ,          s.sub_nbr
                                    ORDER BY     s.step_nbr
                                  )     AS last_step_finished
         FROM     steps s
         );I know I've had inconsistent datatype errors before with case statements in this particular 8i database, but I can't seem to figure out why I'm getting one this time. I think it has something to do with the NULL values that can occur for last_step_finished. Also, if I change the case statement to:
    ,     CASE
              WHEN     start_dt - last_step_finished     < 0
              THEN     NULL
              ELSE     start_dt - last_step_finished  -- THIS LINE GENERATES THE ORA-00932 ERROR
         END     the query runs just fine. But, I don't want NULL, I want 0. In the next level of this query, I will be taking averages by item_id/step_nbr, and I want the 0's to be included in the average. (NULL values, as far as I can tell, would be excluded. AVG(NULL, 1, 2) =AVG (1,2) = 1.5 NOT AVG(0,1,2) = 1).
    Thanks in advance!

    Thanks, TO_NUMBER did the trick. Since you didn't state in your post where to use TO_NUMBER, here is my final solution, in case anyone in the future looks through this thread to find an answer to their question:
    SELECT     item_id
    ,     ord_nbr
    ,     sub_nbr
    ,     step_nbr
    ,     CASE
              WHEN     start_dt - last_step_finished     < 0
              THEN     0
              ELSE     TO_NUMBER(start_dt - last_step_finished)
         END                         AS days_in_queue
    FROM     (
         SELECT     s.*
         ,     LAG (s.finish_date)     OVER     ( PARTITION BY  s.item_id
                                    ,          s.ord_nbr
                                    ,          s.sub_nbr
                                    ORDER BY     s.step_nbr
                                  )     AS last_step_finished
         FROM     steps s
         );Edited by: user11033437 on Jun 27, 2011 11:17 AM
    I see you edited your post to add TO_NUMBER to it.

  • Wildcard value in case statement

    Hi,
     How would I use a wildcard value in my case statement. I evaluate a string value of numbers and try to do this "1234***" but it doesn't work. "***" represents any 3 number. 
    Please advise.
    Thanks
    dphan128

    You could mark one of the cases as "Default", and that case will execute when no others match. If you want to explicitly run that case only if the first 4 numbers are "1234", then you need to pre-process the string.
    See here for the same exact question.

  • Performance in a case statement

    Hi
    I have a query like this -
    SELECT
    MAX(CASE WHEN field1 = 'xyz' THEN field2 END) AS alias1,
    MAX(CASE WHEN field2 = 'abc' THEN field2 END) AS alias2
    FROM table1
    WHERE
    field1 = 'xyz' and
    field2 = 'abc' and
    some fixed condition
    Please note that the conditions in where clauses are same as corresponding case statements apart from one fixed condition.
    In the actual query there are ~15 such conditions and 15 case statements for each selected field.
    I have put the where clause to reduce the number of rows getting returned from the table.
    My question is - what happens in terms of performance if I remove the where clause here. In other words will above query perform better or same as this
    SELECT
    MAX(CASE WHEN field1 = 'xyz' THEN field2 END) AS alias1,
    MAX(CASE WHEN field2 = 'abc' THEN field2 END) AS alias2
    FROM table1
    WHERE
    some fixed condition
    Thanks
    Kumar

    It really depends on what you query is trying to accomplish. The two queries are not the same. Your first will select rows where both field1 = 'xyz' and field2 = 'abc', while the second would also return rows where, for example, field1 = 'xyz' and field2 = 'def'. In which case, your alias2 would be null.
    John

  • 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 with Subquery

    Case statement with Subquery
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?

    Hi,
    Welcoem to the forum!
    972471 wrote:
    Case statement with SubqueryHow does a sub-query, or a query, figure in this question?
    Be more specific. Post a complete script that you tried, and what output you expected from it. If you got an error, post the complete error message, including line numbers.
    See the forum FAQ {message:id=9360002}
    select case when condition then
    i want to execute qr1
    else qr2
    How to implement this ?Here's one way:
    BEGIN
        CASE
            WHEN  1 = 1
            THEN
                dbms_output.put_line ('Condition was TRUE');
            ELSE
                dbms_output.put_line ('Condition was FALSE or UNKNOWN');
        END CASE;
    END;
    /If the condition involves a query, then you may have to select the results into a variable first, and then use the variable in the CASE statement.

  • SSIS: Using CASE Statement Within A Derived Column Transformation Expression

    The following is my Data Flow:
    Ole DB Source > Copy Column > Derived Column >Ole DB Command
    My OLE DB Source has the following SQL command task:
    SELECT *
    FROM Repair R
    LEFT OUTER JOIN Vehicle V
    ON R.SN = V.SN
    AND R.Reg = V.Reg
    LEFT OUTER JOIN Product P
    ON R.PID = P.PID
    This yields a column of concern for me named PartNumber, which are represented by the following 2 formats:
    The following are my Copied Columns:
    Input Column = PartNumber
    Output Alias = Copy of PartNumber
    The following are my Derived Column expressions:
    Derived Column Name Derived Column Expression
    Name Replace 'PartNumber' LEFT(PartNumber,FINDSTRING(PartNumber,"-",1) - 1)
    Copy of Name Replace 'Copy of PartNumber' RIGHT([Copy of PartNumber],LEN([Copy of PartNumber]) - FINDSTRING([Copy of PartNumber],"-",1))
    So My PartNumber Column is Replaced with ######### of type
    string and the Copy of PartNumber column is replaced with
    #### of type int
    As I stated earlier PartNumber also is in the format of %-%-% which raises the following question:
    How can I replace PartNumber Column with NULL if in format of
    %-%-% and take the PartNumber and put it in new column named
    SubPart while keeping all the logic within SSIS objects?
    So in essence I want to do something like the following:
    PartNumber = CASE
    WHEN 'PartNumber' LIKE '%-%-%'
    THEN ABC = PartNumber AND PartNumber IS NULL
    END
    I have tried the case statement in a Derived Column expression and is not working. Would I add the
    CASE statement to the SQL command task in my OLE DB Source?
    I hope this question is concise for you.

    If it must be in SSIS, I would put a derived column stage earlier in the process to do my check for me. Then in the second derived column stage do the check against each output column (part, subpart, copy of part) to decide which way it goes
    PS Not sure if it's a 2012 function but SSIS has a function called TOKEN that will allow you to pull your subparts for you rather than the left/right you are doing

Maybe you are looking for

  • Dynamic Tempo Mapping In Audition CC

    Hello Adobe, I've seen various forum postings about this in the past, but nothing very recently.  I just downloaded the latest version of Audition CC.  It looks great, a lot of cool new features, but I wanted to reiterate on past postings that dynami

  • The icons and boxes around the icons of web pages are not showing

    Imagine when looking at the Mozilla "enter your username and password page" all you see is text but no boxes around the fields. Well...that is what I see. I see "Enter your username" but next to it is no boxes around the field to enter the username.

  • Stale Date Error in Advance Table...Its Very Urgent

    Hi All, I am getting stale data in Advance Table Regions. I am having to buttons, if I selected button1 it shows one advance table region (with show All and Hide All options) it is showing correct data in the details view. If I select button2 and new

  • Font Book.............I give up!!!! Please help.....................

    PLEASE HELP!!! I've spent my day trying to resolve why I can't open my Helvetica Neue fonts. I'm working in Adobe Illustrator 12.0.3 (CS2) on a file that until today opened fine. My client had sent me a copy of OpenType Helvetica Neue last week askin

  • Need nitty-gritty help uninstalling

    My computer crashed during an iTunes upgrade... so then, I've got a/some critical file(s) missing. ITunes can't run w/o the file(s), can't uninstall (using the Windows uninstaller) without the file(s), and can't re-install because it detects a previo