NVL help

Hi,
My query is below, but in the report, null value came out blank, i even change in the Data Model, and it doen't work. help please
SELECT ALL NVL(SUM(ITEM_MASTER.ADJUSTED_COST),0) cost, ITEM_SCANNED.CASE_BARCODE, CASE_TYPES.DESCRIPTION, NVL(sum(ITEM_SCANNED.QTY_IN_LOC),0) Quantity
FROM ITEM_SCANNED, ITEM_MASTER, MEMO_ITEMS_VIEW, CASES, CASE_TYPES
WHERE

Could you post example data:
[email protected]> create table t(i number);
Table created.
[email protected]> insert into t values(1);
1 row created.
[email protected]> insert into t values(null);
1 row created.
[email protected]> commit;
Commit complete.
[email protected]> select sum(i) from t;
SUM(I)
1
Best Regards
Krystian Zieja / mob

Similar Messages

  • Need help getting totals.  does nvl(' ',0)  return a numeric 0 ?

    I am having problems trying to total some columns and I think it has to do with the sql not seeing the information as numeric.
    I created this to test. With this nvl it displays ORA-01722: invalid number
    select bcount.b1,bcount.b2,bcount.b3,acount.a1,acount.a2,acount.a3,bcount.b1 + bcount.b2 + bcount.b3 + acount.a1 + acount.a2 + acount.a3 mytot
    from
    (select 10 item,nvl(' ',0) b1,2 b2, 3 b3 from dual) bcount,
    (select 10 item,1 a1,2 a2, 3 a3 from dual) acount
    where bcount.item = acount.item
    Actual sql
    select emp.file_no,nvl(reg.regcount,0) regcount,nvl(ded.dedcount,0) dedcount,rehrg.rehrgcount,rop.ropcount,vs.vscount,
    wtc.wtccount,timeline.tlcount,hl.hlcount,addl.addcount,op.opcount,oi.oicount,opoi.opoicount,
    regcount + dedcount mtotal
    if regcount = 8 and dedcount = 0 I get blank for my mtotal.
    I think its because the nvl returns a 0 but the sql does not know this to be a number.
    What do I do?
    Howard

    I hope this helps and thanks
    results
    FILE_NO REGCOUNT DEDCOUNT REHRGCOUNT ROPCOUNT VSCOUNT WTCCOUNT TLCOUNT HLCOUNT ADDCOUNT OPCOUNT OICOUNT OPOICOUNT MTOTAL
    898989 8 - - - 8 - 8 - 4 - - - -
    sql
    select emp.file_no,reg.regcount,ded.dedcount,rehrg.rehrgcount,rop.ropcount,vs.vscount,
    wtc.wtccount,timeline.tlcount,homeless.hlcount,addl.addcount,op.opcount,oi.oicount,opoi.opoicount,
    reg.regcount + ded.dedcount mTotal
    from
    (select file_no,OU_NAME,employee_level_id from tbl_employee where file_no is not null and employee_level_id = 4) emp,
    (select tca.assign_to_ahs,case.reh_reop_id,count(*) regcount from tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and case.reh_reop_id is null
    group by tca.assign_to_ahs,case.reh_reop_id) reg, -- regular
    (select tca.assign_to_ahs,case.reh_reop_id,count(*) ropcount from tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and case.reh_reop_id = 1
    group by tca.assign_to_ahs,case.reh_reop_id) rop, -- rop
    (select tca.assign_to_ahs,case.reh_reop_id,count(*) rehrgcount from tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and case.reh_reop_id = 2
    group by tca.assign_to_ahs,case.reh_reop_id) rehrg, --
    (select tca.assign_to_ahs,tait.issue_type_id,count(*) dedcount from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and issue_type_id in (46,47)
    group by tca.assign_to_ahs,tait.issue_type_id) ded,
    (select tca.assign_to_ahs,count(*) as VSCOUNT
    from tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and case.VS_OTHER_CNTY_AT_LA is not null
    group by tca.assign_to_ahs) vs,
    (select tca.assign_to_ahs,count(*) wtccount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.issue_type_id = 13 -- need to change to 49
    group by tca.assign_to_ahs, tait.issue_type_id) wtc,
    (select tca.assign_to_ahs,tait.aid_type_id,count(*) tlcount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.aid_type_id = 1 -- calworks
    group by tca.assign_to_ahs,tait.aid_type_id) timeline,
    (select tca.assign_to_ahs,tait.issue_type_id,count(*) hlcount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.issue_type_id = 38 -- homeless
    group by tca.assign_to_ahs,tait.issue_type_id) homeless,
    (select tca.assign_to_ahs,count(*) addcount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.ADDTL_FILING_DATE is not null
    group by tca.assign_to_ahs) addl,
    (select tca.assign_to_ahs,tait.aid_type_id,tait.issue_type_id,count(*) opcount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.aid_type_id not in (2,3,4,5,6,7,8,9,10) -- get only 1 calworks
    and tait.issue_type_id in (53,60,70)
    group by tca.assign_to_ahs,tait.aid_type_id,tait.issue_type_id) op,
    (select tca.assign_to_ahs,tait.aid_type_id,tait.issue_type_id,count(*) oicount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.aid_type_id not in (1,3,4,5,6,7,8,9,10) -- get only 2 foodstamps
    and tait.issue_type_id in (53,60,70)
    group by tca.assign_to_ahs,tait.aid_type_id,tait.issue_type_id) oi,
    (select tca.assign_to_ahs,tait.issue_type_id,count(*) opoicount
    from tbl_aid_issue_types tait,
    tbl_cases case,tbl_case_assigned tca
    where case.createdate between TO_DATE(:P_from,'mm/dd/yyyy') and to_date(:P_to,'mm/dd/yyyy')
    and tca.ats_no = case.ats_no
    and tca.ats_no = tait.ats_no
    and tait.aid_type_id not in (3,4,5,6,7,8,9,10) -- get only 1 and 2 foodstamps and calworks
    and tait.issue_type_id in (53,60,70)
    group by tca.assign_to_ahs,tait.issue_type_id) opoi
    where emp.OU_NAME = reg.assign_to_ahs(+)
    and reg.assign_to_ahs = rop.assign_to_ahs(+)
    and reg.assign_to_ahs = rehrg.assign_to_ahs(+)
    and reg.assign_to_ahs = ded.assign_to_ahs(+)
    and reg.assign_to_ahs = vs.assign_to_ahs(+)
    and reg.assign_to_ahs = wtc.assign_to_ahs(+)
    and reg.assign_to_ahs = timeline.assign_to_ahs(+)
    and reg.assign_to_ahs = homeless.assign_to_ahs(+)
    and reg.assign_to_ahs = addl.assign_to_ahs(+)
    and reg.assign_to_ahs = op.assign_to_ahs(+)
    and reg.assign_to_ahs = oi.assign_to_ahs(+)
    and reg.assign_to_ahs = opoi.assign_to_ahs(+)
    and emp.file_no = '898989'
    order by emp.file_no
    Edited by: csphard on May 9, 2009 8:36 PM

  • Help with nvl and to_char in query

    Hi, I was wondering if anyone could help me with finding the reason that the following query gives me an invalid number error:
    SELECT     to_char('dd/mm/yyyy',nvl(co_modified_date,co_added_date)) as d1
    FROM     co_allergies_record_upd
    WHERE          co_allergies_record_upd_id IN (
    SELECT co_allergies_record_upd_id
    FROM     co_allergies_link_upd
    WHERE     co_allergies_rec_id = 42
    ORDER BY     nvl(co_modified_date,co_added_date) DESC
    Specifically, it is the nvl(co_modified_date,co_added_date) which is causing the error. The co_added_date has a NOT NULL constraint, and both are date fields. What could be causing the error?

    You have missed format and data argument places in to_char():
    SELECT to_char('dd/mm/yyyy',nvl(co_modified_date,co_added_date)) as d1
    FROM co_allergies_record_upd
    WHERE co_allergies_record_upd_id IN (
    SELECT co_allergies_record_upd_id...
    SQL> select to_char('dd/mm/yyyy',sysdate) from dual;
    select to_char('dd/mm/yyyy',sysdate) from dual
    ERROR at line 1:
    ORA-01722: invalid number
    SQL> select to_char(sysdate,'dd/mm/yyyy') from dual;
    TO_CHAR(SY
    20/12/2006Rgds.

  • Help with outer joins? NVL?

    HI,
    I have two reports I am creating, one for October and one for november.
    The query is below so you can see what I am trying to do. There is a difference of about two hundred accounts, I want to see which accounts were in October and not in November. How can I view the different accounts and list them along side the november accounts or if this can't be done, how can I at least see the accounts which differ between the months? I am thinking that I need to build one report as an inline-view in the from clause, and then do an outerjoin on that aliased table, but am not sure on the syntax. Also, how can I use the NVL function to put in a value for the NULL columns?
    select d.month,
         a.account_no,
         x.first_name||', '||x.last_name,
         d.product,
         d.cycle_forward ,
         d.overusage,
         d.logins,
         d.seconds/3600 hours
    from brio_broadcast.monthly_dial_revenue d,
         pin.account_nameinfo_t x,
         pin.account_t a
    where month = '2002-10'
    and a.POID_ID0 = d.ACCOUNT_OBJ_ID0
    and a.poid_id0 = x.obj_id0     
    order by a.account_no;

    SELECT   o.month,
             o.account_no,
             o.name,
             o.product,
             o.cycle_forward,
             o.overusage,
             o.logins,
             o.hours
    FROM     (SELECT   d.month,
                       a.account_no,
                       x.first_name || ', ' || x.last_name AS name,
                       d.product,
                       d.cycle_forward ,
                       d.overusage,
                       d.logins,
                       d.seconds / 3600 hours
              FROM     brio_broadcast.monthly_dial_revenue d,
                       pin.account_nameinfo_t              x,
                       pin.account_t                       a
              WHERE    d.month = '2002-11'
              AND      a.poid_id0 = d.account_obj_id0
              AND      a.poid_id0 = x.obj_id0) n,
             (SELECT   d.month,
                       a.account_no,
                       x.first_name || ', ' || x.last_name AS name,
                       d.product,
                       d.cycle_forward ,
                       d.overusage,
                       d.logins,
                       d.seconds / 3600 hours
              FROM     brio_broadcast.monthly_dial_revenue d,
                       pin.account_nameinfo_t              x,
                       pin.account_t                       a
              WHERE    d.month = '2002-10'
              AND      a.poid_id0 = d.account_obj_id0
              AND      a.poid_id0 = x.obj_id0) o
    WHERE    o.account_no = n.account_no (+)
    AND      n.account_no IS NULL
    UNION ALL
    SELECT   n.month,
             n.account_no,
             n.name,
             n.product,
             n.cycle_forward,
             n.overusage,
             n.logins,
             n.hours
    FROM     (SELECT   d.month,
                       a.account_no,
                       x.first_name || ', ' || x.last_name AS name,
                       d.product,
                       d.cycle_forward ,
                       d.overusage,
                       d.logins,
                       d.seconds / 3600 hours
              FROM     brio_broadcast.monthly_dial_revenue d,
                       pin.account_nameinfo_t              x,
                       pin.account_t                       a
              WHERE    d.month = '2002-10'
              AND      a.poid_id0 = d.account_obj_id0
              AND      a.poid_id0 = x.obj_id0) o,
             (SELECT   d.month,
                       a.account_no,
                       x.first_name || ', ' || x.last_name AS name,
                       d.product,
                       d.cycle_forward ,
                       d.overusage,
                       d.logins,
                       d.seconds / 3600 hours
              FROM     brio_broadcast.monthly_dial_revenue d,
                       pin.account_nameinfo_t              x,
                       pin.account_t                       a
              WHERE    d.month = '2002-11'
              AND      a.poid_id0 = d.account_obj_id0
              AND      a.poid_id0 = x.obj_id0) n
    WHERE    n.account_no = o.account_no (+)
    AND      o.account_no IS NULL
    ORDER BY 2

  • QUERY HELP!!! trying to create a query

    i'm creating a summary report
    i have a table with sale dates
    for example i have a table tab_1 and column saleDate as
    saleDat
    1923
    1936
    1945
    2003
    2005
    saleDate contains years and there are some missing years where no sale
    was made
    My report has to display years starting from earliest year
    so i have to create a query that starts with 1923
    but the problem is that I have to have years that are not in table.
    for example i have to display years 1924 which is not in table
    so the part of report has to look like
    1923 blah blah summary.........
    1924 "
    1925
    1926
    2005
    2006
    upto current year (2006 may not be in the table, but i have to display)
    i just need to know the query that can query all the years starting from
    the ealiest saleDate to current year
    thanks in advance

    Please write the query in the following form:
    SELECT a.year, --- place other columns from your table.
    FROM (SELECT (:start_num + rownum) year
    FROM all_tab_columns
    WHERE :start_num + rownum <= :end_num) a,
    tab_1 b
    WHERE a.year = b.saleDat(+);
    Note:
    1) if your start year and end year are 1923 and 2006. Then input as below:
    :start_num = 1922
    :end_num = 2006
    2) Since for some of the years (1924 etc) may not be there in your so you may need to use NVL to print proper indicators.
    3) If you have more than one record in tab_1 for a particular year then group them based year and then use it.
    Hope this helps.
    - Saumen.

  • Help needed in getting the previous Quarter Data

    Hello folks,
    I have this procedure where i have to modify the current procedure in the following manner:
    I need to get rid of the variables p_start and p_end so that i cannot see them in the crystal report and include the Frequency in the procedure to get the Data based on the Dates.
    and Main requirement is" If the Frequency is Quarterly " it should get the previous quarter Data, if "Frequency is monthly" it should return the previous month data.Can anyone please let me know where shud i make changes. Am including the procedure for refernce. Any help is appreciated
    Thanks a millioin,
    CREATE OR REPLACE PROCEDURE hcsc_recovery_report_h(report_record in out cr_return_types.gen_cursor,
    p_start       string,
    p_end         string)
    IS
    v_startdate date;
    v_enddate date;
    BEGIN
    v_startdate := to_date(p_start, 'YYYY/MM');
    v_enddate := last_day(to_date(p_end, 'YYYY/MM'));
    open report_record for
    select --distinct r.recovery_id
    r.event_id,
    r.event_case_id,
    c.client_id,
    c.client_code,
    c.client_name,
    b.branch_group_code,
    b.branch_group_description,
    g.employer_group_code,
    g.employer_group_name,
    e.client_policy_identifier,
    e.date_of_incident,
    e.event_type_code,
    sum(nvl(r.amount, 0)) as amt_received,
    nvl(sum(case
    when r.amount >= 0 then
    rd.fees
    else
    rd.fees * (-1)
    end),
    0) as fees,
    ec.close_date, *001* commented
    (case
    when ec.close_date <= to_date(to_char(v_enddate, 'MMDDRRRR') || '235959',
    'MMDDRRRR HH24MISS') then
    ec.close_date
    else
    null
    end) as close_date, --*001*  added
    get_case_value(ec.event_id, ec.event_case_id, v_enddate) as case_value,
    nvl(etl.fee_percent_flag, 'N') workmans_comp,
    max(to_char(r.recovery_date, 'FMMonthYYYY')) Year_Month,
    max(to_char(r.recovery_date, 'YYYYMM')) Y_M,
    max(to_date(to_char(r.recovery_date, 'MMYYYY'), 'MM/YYYY')) date_MY
    from recovery r,
    recovery_detail rd,
    event e,
    client c,
    branch_group b,
    employer_group g,
    event_case ec,
    event_type_lookup etl
    where r.event_id = e.event_id
    and r.event_case_id = ec.event_case_id
    and ec.event_id = e.event_id
    and rd.recovery_id(+) = r.recovery_id
    and r.recovery_date between v_startdate and
    to_date(to_char(v_enddate, 'MMDDRRRR') || '235959',
    'MMDDRRRR HH24MISS')
    and e.client_id = c.client_id
    and g.client_id = c.client_id
    and b.client_id = c.client_id
    and g.employer_group_id(+) = e.employer_group_id
    and b.branch_group_id(+) = g.branch_group_id
    and e.event_type_code = etl.event_type_code -- SST 130852 04/14/09
    group by r.event_id,
    r.event_case_id,
    c.client_id,
    c.client_code,
    c.client_name,
    b.branch_group_code,
    b.branch_group_description,
    g.employer_group_code,
    g.employer_group_name,
    e.client_policy_identifier,
    e.date_of_incident,
    e.event_type_code,
    ec.close_date,
    get_case_value(ec.event_id, ec.event_case_id, v_enddate),
    nvl(etl.fee_percent_flag, 'N')
    having sum(nvl(r.amount, 0)) <> 0
    order by c.client_code,
    b.branch_group_code,
    g.employer_group_code,
    r.event_case_id;
    Edited by: user11961230 on Oct 20, 2009 9:02 AM

    user11961230 wrote:
    1. I want to get rid of the p_start and p_end. So how do i declare the v_startdate and v_enddate in the following part?
    v_startdate := to_date(p_start, 'YYYY/MM');
    v_enddate := last_day(to_date(p_end, 'YYYY/MM'));I'm not sure what you mean by "declare".
    In PL/SQL, "declare" means state (at the beginning of a block) that there will be a certain variable with a certain name (such as v_startdate) and datatype (such as DATE). You're already declaring the variables v_startdate and v_enddate correctly, right before the BEGIN statement.
    Declaring a variable is not the same as initializing it, that is, giving it a value for the first time. Your next question seems to be about initializing..
    2. where exactly shud i include the logic that u have mentioned. sorry a dumb questionIn place of the two assignment statments that reference p_start and p_end.
    3. This time am gonna use frequency instead of report_type so that i will get rid of the p_start and p_end from the procedure.Do you mean you want to pass an argument (called frequency) that tells if you want a quarterly or a mionthly report, just like the variable report_type in my example?
    If so, replace report_type in my example with frequency.
    I think you want something like this:
    CREATE OR REPLACE PROCEDURE hcsc_recovery_report_h
    (      report_record         in out     cr_return_types.gen_cursor
    ,      frequency         IN           VARCHAR2
    IS
         -- Declare local variables:
         v_startdate     date;
         v_enddate      date;
    BEGIN
         -- Initialize v_startdate and v_enddate, depending on frequency
         IF  frequency = 'QUARTERLY'
         THEN
              v_startdate := TRUNC ( ADD_MONTHS (SYSDATE, -3)
                                           , 'Q'
              v_enddate := TRUNC (SYSDATE, 'Q');
         ELSIF  frequency = 'MONTHLY'
         THEN
              v_startdate := TRUNC ( ADD_MONTHS (SYSDATE, -1)
                             , 'MM'
              v_enddate := TRUNC (SYSDATE, 'MM');
         END IF;
         --   Subtract one second from v_enddate
              v_enddate := v_enddate - ( 1
                                            / (24 * 60 * 60)
         open report_record for
         select --distinct r.recovery_id
                r.event_id,
         and     r.recovery_date  BETWEEN  v_startdate     
                         AND       v_enddate
         ...When you post formatted text on this site (and code should always be formatted), type these 6 characters:
    (small letters only, inside curly brackets) before and after sections of formatted text, to preserve spacing.
    Edited by: Frank Kulash on Oct 20, 2009 2:37 PM
    Changed query to use BETWEEN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Help needed in writing SQL CASE or DECODE statement

    Hi experts,
    I need to write a SQL to select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.
    Here is my sample data
    PRODORDER_               
    order_num     cntry_cde     Prod_id     Qty
    100     US     A1     5
    101     US     A2     10
    102     AU     A3     4
    103     AU     A4     9
    104     IN     A5     3
    PRODORDER_TXT_               
    order_num     cntry_cde     Prod_id     
    100     US     A1     
    101     US     A2     
    102     NZ     A3     
    103     AU     A4     
    104          A5     
    Here is the requirement,
    1) If the cntry_cde in PROD_ORDER is same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
    2) If they are different, pick the country code from PROD_ORDER_TXT (order 102, AU <> NZ)
    3) If they are different and PROD_ORDER_TXT.cntry_cde is NULL, I cannot use it as cntry_cde in my report (order 104). It happenend just because of the bad data at source.
    I cannot avoid it. Then simply use the cntry_cde from PROD_ORDER
    Output expected
    100     US     A1     5
    101     US     A2     10
    102     NZ     A3     4 -- AU changed to NZ
    103     AU     A4     9
    104     IN     A5     3 -- IN retained as PROD_ORDER_TXT.cntry_cde is null
    sample table creation and insert statements are below
    create table prod_order
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6),
    qty number)
    create table prod_order_txt
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6))
    insert into prod_order values (100, 'US', 'A1',5);
    insert into prod_order values (101, 'US', 'A2',1);
    insert into prod_order values (102, 'AU', 'A3',4);
    insert into prod_order values (103, 'AU', 'A4',9);
    insert into prod_order values (104, 'IN', 'A5',3);
    insert into prod_order_txt values (100,'US','A1');
    insert into prod_order_txt values (101,'US','A2');
    insert into prod_order_txt values (102,'NZ','A3');
    insert into prod_order_txt values (103,'AU','A4');
    insert into prod_order_txt values (104,NULL,'A5');
    commit;
    Thanks for your help in advance
    Edited by: sarvan on Mar 28, 2012 1:39 PM

    Hello
    Thank you for posting all of the ddl and test data along with your expected output - very helpful!. One small point would be to remember to type {noformat}{noformat} before and after any section of code or data in your post so that formatting is retained.  Anyway, this should be a simple join and a combination of CASE and NVL...Select
    po.order_num,
    CASE
    WHEN po.cntry_cde != NVL(pot.cntry_cde,po.cntry_cde)
    THEN
    pot.cntry_cde
    ELSE
    po.cntry_cde
    END cntry_code,
    po.prod_id,
    po.qty
    FROM
    prod_order po
    JOIN
    prod_order_txt pot
    ON
    ( po.order_num = pot.order_num
    ORDER_NUM CN PROD_I QTY
    100 US A1 5
    101 US A2 1
    102 NZ A3 4
    103 AU A4 9
    104 IN A5 3
    5 rows selected.
    HTH
    David
    Edited by: Bravid on Mar 28, 2012 8:32 AM
    corrected !=                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Help on Decode function in Toplink

    Hi,
    I have a sql statement like this:
    select field1, field2 from table1 where upper(field1) like 'ADELPHI%'
    order by field1,
    DECODE(TO_CHAR(NVL(LENGTH(TRANSLATE(field1,'A1234567890','A')),0)),'0',LPAD(field1 ,50),field1)
    How would I converted this to toplink query?
    I appreciate any help you guys can extend to me.

    I just spent about a half hour writing a response here, and the damned forum timed out on me. #$^!@$#!^!@$%.
    Here is the summary:
    - Using TL Expressions and EJBQL are to allow Java developers who don't necessarily know SQL to write queries and to facilitate portability/maintainability of the apps. For REALLY gnarly queries, using TL Expressions or EJBQL won't buy you this. I.e., SQL is more expressive and easier for this kind of quer, so it's not easier to use, and frankly with the code needed to support it, it won't be easier to maintain or port... Bottom line, maybe SQL is the best option here?
    - If you want to experiment, search for TO_CHAR on this forum, and the decode function in the javadoc. You need to build the expressions in pieces and you can treat expressions as arguments to other expressions -- in other words, you can build an expression like builder.leftPad(50, builder.get("field1")) and use it as one of the key/values in the hashtable to the decode function (in theory).
    - Don

  • Help on converting query with case into decode

    Hi Can anyone help me to revise my plsql query? Below is a portion og the procedure I am changing to meet Oracle 8i.
    SELECT SUM(NVL(CASE WHEN (weeks='wk1' ) THEN qty END, 0)) wk1,
    SUM(NVL(CASE WHEN (weeks='wk2' ) THEN qty END, 0)) wk2,
    SUM(NVL(CASE WHEN (weeks='wk3' ) THEN qty END, 0)) wk3,
    SUM(NVL(CASE WHEN (weeks='wk4' ) THEN qty END, 0)) wk4
    FROM TABLE1
    Thanks in advance.

    pls try this....(code not tested as no create table etc provided).
    select dt
    from (select decode(is_tbd,
    'yes',
    decode(is_tbd_order,
    'no',
    'tbd',
    decode(sign(ex_fac_date - (v_asofdate + 131)),
    1,
    'tbd',
    null),
    decode(sign(ex_fac_date - (v_asofdate + 131)),
    1,
    'tbd',
    null)),
    null) dt
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate - 1)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 5)),
    0,
    decode(is_tbd, 'no', 'wk1', null),
    null),
    null)
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate + 6)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 12)),
    0,
    decode(is_tbd, 'no', 'wk2', null),
    null),
    null)
    from dual
    select decode(sign(ex_fac_date - (v_asofdate + 13)),
    1,
    decode(sign(ex_fac_date - (v_asofdate + 19)),
    0,
    decode(is_tbd, 'no', 'wk3', null),
    null),
    null)
    from dual
    union all
    select decode(sign(ex_fac_date - (v_asofdate - 1)),
    0,
    decode(is_tbd, 'no', 'past_due', null),
    null)
    from dual
    where dt is not null
    Regards

  • Help to read TKPROF file in correct way

    Hi,
    I need help to understand what is going wrong.
    I have one query executed with different bind variables values.
    Case 1: Execution time is about 7.5 hours when :v6=:v3=:v4 (these are date bind variables values)
    Case 2: Execution time is about 2 minuteswhen :v6 differs from :v3 and :v4, but :v3=:v4 (these are date bind variables values)
    You can see TKPROF files below
    Case1
    SELECT EK.SECIK AS EK_SECIK,S.SECIK AS S_SECIK,EK.EQKEY1 AS EK_EQKEY1,EK.FROMDATE AS EK_FROMDATE,S.SECSHORT AS S_SECSHORT,S.SECNAME AS S_SECNAME,C.CDY AS C_CDY,C.CSTD AS C_CSTD,P.PAR AS P_PAR,P.PARIK AS P_PARIK,PH.ACCOUNTING_FRAMEWORK AS PH_ACC_FW,NVL(PH.BAL_NOMINAL_NUMBER,0) AS PH_BAL_NOM_NUM,PH.FROM_DATE AS PH_FROM_DATE,PH.POR AS PH_POR,PH.SEC_SHORT_NAME AS PH_SEC_SHORT_NAME,PH.TO_DATE AS PH_TO_DATE,PH.CUSTODY_SHORT_NAME AS PH_CUST_SHORT_NAME,T.TC_CSTD AS T_CSTD,SUM(T.TH_BAL_NOM_NUM) AS T_BAL_NOM_NUM,T.TH_SEC_SHORT_NAME AS T_SEC_SHORT_NAME,PH.POR_REF AS PH_POR_REF,PORT.PORIK AS PORT_PORIK,PORT.PORGRPIK AS PORT_PORGRPIK,PG.PORGRPIK AS PG_PORGRPIK,PG.PORGRPTYPEIK AS PG_PORGRPTYPEIK,PGT.PORGRPTYPEIK AS PGT_PORGRPTYPEIK FROM SCDAT.CUSTODIES C,SCDAT.EQUITYKEYS EK,SCDAT.PARTNERS P,SCDAT.PORTGROUPS PG,SCDAT.PORTGROUPTYPES PGT,(
    SELECT  HOLKEYS.PORIK As POR_REF,
    ACCDEF.ACC As ACCOUNTING_FRAMEWORK,
    NVL(HOLDINGS.BALNOMVAL,0) As BAL_NOMINAL_NUMBER,
    CUSTODIES.CDY As CUSTODY_SHORT_NAME,
    DECODE(HOLDINGS.FINBOOKED,0,'No',1,'Yes',' ') As FINALLY_BOOKED,
    HOLDINGS.FROMDATE As FROM_DATE,
    PORTFOLIOS.POR As POR,
    SECURITIES.SECSHORT As SEC_SHORT_NAME,
    HOLDINGS.TODATE As TO_DATE,
    HOLDINGS.FINBOOKED As X_FINALLY_BOOKED
    FROM HOLDINGS,HOLKEYS,ACCDEF,CUSTODIES,PORTFOLIOS,SECURITIES WHERE HOLDINGS.HOLKEYIK = HOLKEYS.HOLKEYIK AND HOLDINGS.ACCIK = ACCDEF.ACCIK AND HOLKEYS.CDYIK = CUSTODIES.CDYIK AND HOLKEYS.PORIK = PORTFOLIOS.PORIK AND HOLKEYS.SECIK = SECURITIES.SECIK) PH,SCDAT.PORTFOLIOS PORT,SCDAT.SECURITIES S,(SELECT TC.CDY AS TC_CDY,TC.CSTD AS TC_CSTD,SUM(NVL(TH.BAL_NOMINAL_NUMBER,0)) AS TH_BAL_NOM_NUM,TH.FROM_DATE AS TH_FROM_DATE,TH.TO_DATE AS TH_TO_DATE,TH.SEC_SHORT_NAME AS TH_SEC_SHORT_NAME,TH.CUSTODY_SHORT_NAME AS TH_CUST_SHORT_NAME,TP.PAR AS TP_PAR,TP.PARIK AS TP_PARIK FROM SCDAT.CUSTODIES TC,(
    SELECT  ACCDEF.ACC As ACCOUNTING_FRAMEWORK,
    NVL(HOLDINGS.BALNOMVAL,0) As BAL_NOMINAL_NUMBER,
    CUSTODIES.CDY As CUSTODY_SHORT_NAME,
    DECODE(HOLDINGS.FINBOOKED,0,'No',1,'Yes',' ') As FINALLY_BOOKED,
    HOLDINGS.FROMDATE As FROM_DATE,
    SECURITIES.SECSHORT As SEC_SHORT_NAME,
    HOLDINGS.TODATE As TO_DATE,
    HOLDINGS.FINBOOKED As X_FINALLY_BOOKED
    FROM HOLDINGS,HOLKEYS,ACCDEF,CUSTODIES,SECURITIES WHERE HOLDINGS.ACCIK = ACCDEF.ACCIK AND HOLDINGS.HOLKEYIK = HOLKEYS.HOLKEYIK AND HOLKEYS.CDYIK = CUSTODIES.CDYIK AND HOLKEYS.SECIK = SECURITIES.SECIK) TH,SCDAT.PARTNERS TP WHERE TH.CUSTODY_SHORT_NAME = TC.CDY AND TC.CSTD = TP.PARIK AND TH.ACCOUNTING_FRAMEWORK = :v1  AND NOT (NVL(TH.BAL_NOMINAL_NUMBER,0) = :v2 )  AND TH.FROM_DATE <= :v3  AND TH.TO_DATE >= :v4  AND TH.FINALLY_BOOKED = :v5  GROUP BY TC.CDY,TC.CSTD,TH.FROM_DATE,TH.TO_DATE,TH.SEC_SHORT_NAME,TH.CUSTODY_SHORT_NAME,TP.PAR,TP.PARIK) T WHERE EK.SECIK = S.SECIK AND PORT.PORGRPIK = PG.PORGRPIK AND PG.PORGRPTYPEIK = PGT.PORGRPTYPEIK AND S.SECSHORT = PH.SEC_SHORT_NAME AND PH.SEC_SHORT_NAME = T.TH_SEC_SHORT_NAME AND PH.POR_REF = PORT.PORIK AND PH.CUSTODY_SHORT_NAME = C.CDY AND EK.FROMDATE = :v6  AND NOT (EK.EQKEY1 = :v7 )  AND P.PAR = :v8  AND NOT (PGT.PORGRPTYPE IN (:v9 ,:v10  ))  AND PH.ACCOUNTING_FRAMEWORK = :v11   AND NOT (NVL(PH.BAL_NOMINAL_NUMBER,0) = :v12  )  AND PH.FINALLY_BOOKED = :v13   AND EK.FROMDATE >= PH.FROM_DATE AND EK.FROMDATE <= PH.TO_DATE AND C.CSTD = P.PARIK AND C.CSTD = T.TC_CSTD GROUP BY EK.SECIK,S.SECIK,EK.EQKEY1,EK.FROMDATE,S.SECSHORT,S.SECNAME,C.CDY,C.CSTD,P.PAR,P.PARIK,PH.ACCOUNTING_FRAMEWORK,NVL(PH.BAL_NOMINAL_NUMBER,0),PH.FROM_DATE,PH.POR,PH.SEC_SHORT_NAME,PH.TO_DATE,PH.CUSTODY_SHORT_NAME,T.TC_CSTD,T.TH_SEC_SHORT_NAME,PH.POR_REF,PORT.PORIK,PORT.PORGRPIK,PG.PORGRPIK,PG.PORGRPTYPEIK,PGT.PORGRPTYPEIK
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      2.31       2.31          0          0          0           0
    Fetch        1  27398.48   27473.33      11124  250891597          0           0
    total        3  27400.79   27475.65      11124  250891597          0           0
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 171  (SCDAT)
    Number of plan statistics captured: 1
    Rows (1st) Rows (avg) Rows (max)  Row Source Operation
             0          0          0  HASH GROUP BY (cr=0 pr=0 pw=0 time=55 us cost=1976 size=251 card=1)
           123        123        123   NESTED LOOPS  (cr=250890161 pr=11086 pw=0 time=35602462 us)
         38622      38622      38622    NESTED LOOPS  (cr=250858945 pr=8664 pw=0 time=4221237335 us cost=1975 size=251 card=1)
          3945       3945       3945     NESTED LOOPS  (cr=250850948 pr=7835 pw=0 time=1967017649 us cost=1937 size=199 card=1)
          3833       3833       3833      HASH JOIN  (cr=42840 pr=6196 pw=0 time=4910380 us cost=1559 size=1956 card=12)
          8667       8667       8667       NESTED LOOPS  (cr=42834 pr=6192 pw=0 time=4254867 us)
          8667       8667       8667        NESTED LOOPS  (cr=34167 pr=6192 pw=0 time=4232686 us cost=1556 size=2156 card=14)
          8667       8667       8667         NESTED LOOPS  (cr=26520 pr=6192 pw=0 time=4206426 us cost=1542 size=2058 card=14)
          8667       8667       8667          NESTED LOOPS  (cr=16041 pr=6192 pw=0 time=4161555 us cost=1528 size=1806 card=14)
         18060      18060      18060           NESTED LOOPS  (cr=16037 pr=6191 pw=0 time=4575478 us cost=1527 size=36504 card=312)
           177        177        177            NESTED LOOPS  (cr=6846 pr=6091 pw=0 time=3909468 us cost=1350 size=1500 card=15)
           177        177        177             NESTED LOOPS  (cr=6490 pr=6091 pw=0 time=3907157 us cost=1320 size=1245 card=15)
           177        177        177              NESTED LOOPS  (cr=6133 pr=6091 pw=0 time=3905733 us cost=1305 size=585 card=15)
             1          1          1               NESTED LOOPS  (cr=5 pr=2 pw=0 time=17096 us cost=2 size=23 card=1)
             1          1          1                TABLE ACCESS BY INDEX ROWID ACCDEF (cr=2 pr=2 pw=0 time=17079 us cost=1 size=12 card=1)
             1          1          1                 INDEX UNIQUE SCAN ACCDEF_IX1 (cr=1 pr=1 pw=0 time=6675 us cost=0 size=0 card=1)(object id 103661)
             1          1          1                TABLE ACCESS BY INDEX ROWID PARTNERS (cr=3 pr=0 pw=0 time=14 us cost=1 size=11 card=1)
             1          1          1                 INDEX UNIQUE SCAN PARTNERS_IX1 (cr=2 pr=0 pw=0 time=9 us cost=0 size=0 card=1)(object id 91563)
           177        177        177               TABLE ACCESS FULL EQUITYKEYS (cr=6128 pr=6089 pw=0 time=3888376 us cost=1303 size=240 card=15)
           177        177        177              TABLE ACCESS BY INDEX ROWID SECURITIES (cr=357 pr=0 pw=0 time=1127 us cost=1 size=44 card=1)
           177        177        177               INDEX UNIQUE SCAN P_SECURITIES (cr=180 pr=0 pw=0 time=563 us cost=0 size=0 card=1)(object id 93819)
           177        177        177             TABLE ACCESS BY INDEX ROWID SECURITIES (cr=356 pr=0 pw=0 time=2027 us cost=2 size=17 card=1)
           177        177        177              INDEX RANGE SCAN SECURITIES_IX2 (cr=180 pr=0 pw=0 time=1166 us cost=1 size=0 card=1)(object id 93809)
         18060      18060      18060            TABLE ACCESS BY INDEX ROWID HOLKEYS (cr=9191 pr=100 pw=0 time=716100 us cost=13 size=340 card=20)
         18060      18060      18060             INDEX RANGE SCAN I_HOLKEYS_SECPOR (cr=421 pr=100 pw=0 time=689634 us cost=2 size=0 card=23)(object id 98864)
          8667       8667       8667           TABLE ACCESS BY INDEX ROWID CUSTODIES (cr=4 pr=1 pw=0 time=131716 us cost=1 size=12 card=1)
         36120      36120      36120            INDEX RANGE SCAN R_CUSTODIES_CSTD (cr=3 pr=1 pw=0 time=53659 us cost=0 size=0 card=3)(object id 101437)
          8667       8667       8667          TABLE ACCESS BY INDEX ROWID PORTFOLIOS (cr=10479 pr=0 pw=0 time=36782 us cost=1 size=18 card=1)
          8667       8667       8667           INDEX UNIQUE SCAN P_PORTFOLIOS (cr=1812 pr=0 pw=0 time=14722 us cost=0 size=0 card=1)(object id 91399)
          8667       8667       8667         INDEX UNIQUE SCAN P_PORTGROUPS (cr=7647 pr=0 pw=0 time=20935 us cost=0 size=0 card=1)(object id 95885)
          8667       8667       8667        TABLE ACCESS BY INDEX ROWID PORTGROUPS (cr=8667 pr=0 pw=0 time=15075 us cost=1 size=7 card=1)
             3          3          3       VIEW  index$_join$_005 (cr=6 pr=4 pw=0 time=17822 us cost=3 size=45 card=5)
             3          3          3        HASH JOIN  (cr=6 pr=4 pw=0 time=17816 us)
             5          5          5         INDEX FAST FULL SCAN PORTGROUPTYPES_IX1 (cr=3 pr=2 pw=0 time=10731 us cost=1 size=45 card=5)(object id 95857)
             3          3          3         INDEX FAST FULL SCAN P_PORTGROUPTYPES (cr=3 pr=2 pw=0 time=6182 us cost=1 size=45 card=5)(object id 95858)
          3945       3945       3945      VIEW PUSHED PREDICATE  (cr=250808108 pr=1639 pw=0 time=1615801762 us cost=32 size=36 card=1)
          3945       3945       3945       SORT GROUP BY (cr=250808108 pr=1639 pw=0 time=1615792437 us cost=32 size=86 card=1)
          8052       8052       8052        NESTED LOOPS  (cr=250808108 pr=1639 pw=0 time=3245096176 us cost=31 size=86 card=1)
       6879822    6879822    6879822         NESTED LOOPS  (cr=14142041 pr=0 pw=0 time=93287445 us cost=18 size=53 card=1)
          7665       7665       7665          MERGE JOIN CARTESIAN (cr=3980 pr=0 pw=0 time=214689 us cost=5 size=40 card=1)
          7665       7665       7665           NESTED LOOPS  (cr=3846 pr=0 pw=0 time=75334 us cost=3 size=23 card=1)
          3833       3833       3833            TABLE ACCESS BY INDEX ROWID PARTNERS (cr=3842 pr=0 pw=0 time=38447 us cost=2 size=11 card=1)
          3833       3833       3833             INDEX UNIQUE SCAN P_PARTNERS (cr=9 pr=0 pw=0 time=13355 us cost=1 size=0 card=1)(object id 91507)
          7665       7665       7665            TABLE ACCESS BY INDEX ROWID CUSTODIES (cr=4 pr=0 pw=0 time=34661 us cost=1 size=12 card=1)
          7665       7665       7665             INDEX RANGE SCAN R_CUSTODIES_CSTD (cr=3 pr=0 pw=0 time=23093 us cost=0 size=0 card=1)(object id 101437)
          7665       7665       7665           BUFFER SORT (cr=134 pr=0 pw=0 time=128191 us cost=4 size=17 card=1)
          7665       7665       7665            TABLE ACCESS BY INDEX ROWID SECURITIES (cr=134 pr=0 pw=0 time=44771 us cost=2 size=17 card=1)
          7665       7665       7665             INDEX RANGE SCAN SECURITIES_IX2 (cr=109 pr=0 pw=0 time=32098 us cost=1 size=0 card=1)(object id 93809)
       6879822    6879822    6879822          TABLE ACCESS BY INDEX ROWID HOLKEYS (cr=14138061 pr=0 pw=0 time=99798296 us cost=13 size=13 card=1)
      27845684   27845684   27845684           INDEX RANGE SCAN I_HOLKEYS_SECPOR (cr=92999 pr=0 pw=0 time=21161648 us cost=2 size=0 card=23)(object id 98864)
          8052       8052       8052         VIEW PUSHED PREDICATE  VW_GBC_22 (cr=236667503 pr=1677 pw=0 time=1559090990 us cost=13 size=33 card=1)
          8052       8052       8052          SORT GROUP BY (cr=236667503 pr=1677 pw=0 time=1552356479 us cost=13 size=64 card=1)
          8052       8052       8052           HASH JOIN  (cr=236667503 pr=1677 pw=0 time=1518269306 us cost=12 size=64 card=1)
       6879822    6879822    6879822            TABLE ACCESS BY INDEX ROWID ACCDEF (cr=13759644 pr=0 pw=0 time=58085525 us cost=1 size=12 card=1)
       6879822    6879822    6879822             INDEX UNIQUE SCAN ACCDEF_IX1 (cr=6879822 pr=0 pw=0 time=37383556 us cost=0 size=0 card=1)(object id 103661)
         16077      16077      16077            TABLE ACCESS BY INDEX ROWID HOLDINGS (cr=222907859 pr=1677 pw=0 time=1109306657 us cost=10 size=52 card=1)
      13212301   13212301   13212301             INDEX SKIP SCAN P_HOLDINGS (cr=211196455 pr=431 pw=0 time=734718877 us cost=6 size=0 card=3)(object id 2173392)
         38622      38622      38622     INDEX RANGE SCAN R_HOLDINGS_HOLKEYIK (cr=7997 pr=829 pw=0 time=9653175 us cost=2 size=0 card=42)(object id 2173885)
           123        123        123    TABLE ACCESS BY INDEX ROWID HOLDINGS (cr=31216 pr=2422 pw=0 time=40223697 us cost=38 size=52 card=1)
    Case2
    SELECT EK.SECIK AS EK_SECIK,S.SECIK AS S_SECIK,EK.EQKEY1 AS EK_EQKEY1,EK.FROMDATE AS EK_FROMDATE,S.SECSHORT AS S_SECSHORT,S.SECNAME AS S_SECNAME,C.CDY AS C_CDY,C.CSTD AS C_CSTD,P.PAR AS P_PAR,P.PARIK AS P_PARIK,PH.ACCOUNTING_FRAMEWORK AS PH_ACC_FW,NVL(PH.BAL_NOMINAL_NUMBER,0) AS PH_BAL_NOM_NUM,PH.FROM_DATE AS PH_FROM_DATE,PH.POR AS PH_POR,PH.SEC_SHORT_NAME AS PH_SEC_SHORT_NAME,PH.TO_DATE AS PH_TO_DATE,PH.CUSTODY_SHORT_NAME AS PH_CUST_SHORT_NAME,T.TC_CSTD AS T_CSTD,SUM(T.TH_BAL_NOM_NUM) AS T_BAL_NOM_NUM,T.TH_SEC_SHORT_NAME AS T_SEC_SHORT_NAME,PH.POR_REF AS PH_POR_REF,PORT.PORIK AS PORT_PORIK,PORT.PORGRPIK AS PORT_PORGRPIK,PG.PORGRPIK AS PG_PORGRPIK,PG.PORGRPTYPEIK AS PG_PORGRPTYPEIK,PGT.PORGRPTYPEIK AS PGT_PORGRPTYPEIK FROM SCDAT.CUSTODIES C,SCDAT.EQUITYKEYS EK,SCDAT.PARTNERS P,SCDAT.PORTGROUPS PG,SCDAT.PORTGROUPTYPES PGT,(
    SELECT  HOLKEYS.PORIK As POR_REF,
    ACCDEF.ACC As ACCOUNTING_FRAMEWORK,
    NVL(HOLDINGS.BALNOMVAL,0) As BAL_NOMINAL_NUMBER,
    CUSTODIES.CDY As CUSTODY_SHORT_NAME,
    DECODE(HOLDINGS.FINBOOKED,0,'No',1,'Yes',' ') As FINALLY_BOOKED,
    HOLDINGS.FROMDATE As FROM_DATE,
    PORTFOLIOS.POR As POR,
    SECURITIES.SECSHORT As SEC_SHORT_NAME,
    HOLDINGS.TODATE As TO_DATE,
    HOLDINGS.FINBOOKED As X_FINALLY_BOOKED
    FROM HOLDINGS,HOLKEYS,ACCDEF,CUSTODIES,PORTFOLIOS,SECURITIES WHERE HOLDINGS.HOLKEYIK = HOLKEYS.HOLKEYIK AND HOLDINGS.ACCIK = ACCDEF.ACCIK AND HOLKEYS.CDYIK = CUSTODIES.CDYIK AND HOLKEYS.PORIK = PORTFOLIOS.PORIK AND HOLKEYS.SECIK = SECURITIES.SECIK) PH,SCDAT.PORTFOLIOS PORT,SCDAT.SECURITIES S,(SELECT TC.CDY AS TC_CDY,TC.CSTD AS TC_CSTD,SUM(NVL(TH.BAL_NOMINAL_NUMBER,0)) AS TH_BAL_NOM_NUM,TH.FROM_DATE AS TH_FROM_DATE,TH.TO_DATE AS TH_TO_DATE,TH.SEC_SHORT_NAME AS TH_SEC_SHORT_NAME,TH.CUSTODY_SHORT_NAME AS TH_CUST_SHORT_NAME,TP.PAR AS TP_PAR,TP.PARIK AS TP_PARIK FROM SCDAT.CUSTODIES TC,(
    SELECT  ACCDEF.ACC As ACCOUNTING_FRAMEWORK,
    NVL(HOLDINGS.BALNOMVAL,0) As BAL_NOMINAL_NUMBER,
    CUSTODIES.CDY As CUSTODY_SHORT_NAME,
    DECODE(HOLDINGS.FINBOOKED,0,'No',1,'Yes',' ') As FINALLY_BOOKED,
    HOLDINGS.FROMDATE As FROM_DATE,
    SECURITIES.SECSHORT As SEC_SHORT_NAME,
    HOLDINGS.TODATE As TO_DATE,
    HOLDINGS.FINBOOKED As X_FINALLY_BOOKED
    FROM HOLDINGS,HOLKEYS,ACCDEF,CUSTODIES,SECURITIES WHERE HOLDINGS.ACCIK = ACCDEF.ACCIK AND HOLDINGS.HOLKEYIK = HOLKEYS.HOLKEYIK AND HOLKEYS.CDYIK = CUSTODIES.CDYIK AND HOLKEYS.SECIK = SECURITIES.SECIK) TH,SCDAT.PARTNERS TP WHERE TH.CUSTODY_SHORT_NAME = TC.CDY AND TC.CSTD = TP.PARIK AND TH.ACCOUNTING_FRAMEWORK = :v1  AND NOT (NVL(TH.BAL_NOMINAL_NUMBER,0) = :v2 )  AND TH.FROM_DATE <= :v3  AND TH.TO_DATE >= :v4  AND TH.FINALLY_BOOKED = :v5  GROUP BY TC.CDY,TC.CSTD,TH.FROM_DATE,TH.TO_DATE,TH.SEC_SHORT_NAME,TH.CUSTODY_SHORT_NAME,TP.PAR,TP.PARIK) T WHERE EK.SECIK = S.SECIK AND PORT.PORGRPIK = PG.PORGRPIK AND PG.PORGRPTYPEIK = PGT.PORGRPTYPEIK AND S.SECSHORT = PH.SEC_SHORT_NAME AND PH.SEC_SHORT_NAME = T.TH_SEC_SHORT_NAME AND PH.POR_REF = PORT.PORIK AND PH.CUSTODY_SHORT_NAME = C.CDY AND EK.FROMDATE = :v6  AND NOT (EK.EQKEY1 = :v7 )  AND P.PAR = :v8  AND NOT (PGT.PORGRPTYPE IN (:v9 ,:v10  ))  AND PH.ACCOUNTING_FRAMEWORK = :v11   AND NOT (NVL(PH.BAL_NOMINAL_NUMBER,0) = :v12  )  AND PH.FINALLY_BOOKED = :v13   AND EK.FROMDATE >= PH.FROM_DATE AND EK.FROMDATE <= PH.TO_DATE AND C.CSTD = P.PARIK AND C.CSTD = T.TC_CSTD GROUP BY EK.SECIK,S.SECIK,EK.EQKEY1,EK.FROMDATE,S.SECSHORT,S.SECNAME,C.CDY,C.CSTD,P.PAR,P.PARIK,PH.ACCOUNTING_FRAMEWORK,NVL(PH.BAL_NOMINAL_NUMBER,0),PH.FROM_DATE,PH.POR,PH.SEC_SHORT_NAME,PH.TO_DATE,PH.CUSTODY_SHORT_NAME,T.TC_CSTD,T.TH_SEC_SHORT_NAME,PH.POR_REF,PORT.PORIK,PORT.PORGRPIK,PG.PORGRPIK,PG.PORGRPTYPEIK,PGT.PORGRPTYPEIK
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.00       0.00          0          0          0           0
    Execute      1      2.34       2.34          0          0          0           0
    Fetch        2     39.31     131.53      17885   17489238          0        2039
    total        4     41.66     133.88      17885   17489238          0        2039
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 171  (SCDAT)
    Number of plan statistics captured: 1
    Rows (1st) Rows (avg) Rows (max)  Row Source Operation
          2039       2039       2039  HASH GROUP BY (cr=17489238 pr=17885 pw=0 time=131529969 us cost=2062 size=251 card=1)
        166827     166827     166827   NESTED LOOPS  (cr=17489238 pr=17885 pw=0 time=157379020 us cost=2061 size=251 card=1)
          2043       2043       2043    NESTED LOOPS  (cr=143626 pr=5419 pw=0 time=19393083 us cost=2003 size=215 card=1)
         11156      11156      11156     HASH JOIN  (cr=53391 pr=2600 pw=0 time=4865614 us cost=1559 size=1956 card=12)
         11156      11156      11156      NESTED LOOPS  (cr=53385 pr=2600 pw=0 time=4585085 us)
         11156      11156      11156       NESTED LOOPS  (cr=42229 pr=2600 pw=0 time=4563404 us cost=1555 size=2156 card=14)
         11156      11156      11156        NESTED LOOPS  (cr=32467 pr=2600 pw=0 time=4543117 us cost=1541 size=2058 card=14)
         11156      11156      11156         NESTED LOOPS  (cr=19234 pr=2600 pw=0 time=4506220 us cost=1527 size=1806 card=14)
         24211      24211      24211          NESTED LOOPS  (cr=19230 pr=2600 pw=0 time=4417211 us cost=1526 size=36504 card=312)
           193        193        193           NESTED LOOPS  (cr=6926 pr=2556 pw=0 time=4371848 us cost=1350 size=1500 card=15)
           193        193        193            NESTED LOOPS  (cr=6537 pr=2556 pw=0 time=4370289 us cost=1320 size=1245 card=15)
           193        193        193             NESTED LOOPS  (cr=6148 pr=2556 pw=0 time=4369122 us cost=1305 size=585 card=15)
             1          1          1              NESTED LOOPS  (cr=5 pr=1 pw=0 time=6568 us cost=2 size=23 card=1)
             1          1          1               TABLE ACCESS BY INDEX ROWID ACCDEF (cr=2 pr=1 pw=0 time=6556 us cost=1 size=12 card=1)
             1          1          1                INDEX UNIQUE SCAN ACCDEF_IX1 (cr=1 pr=1 pw=0 time=6551 us cost=0 size=0 card=1)(object id 103661)
             1          1          1               TABLE ACCESS BY INDEX ROWID PARTNERS (cr=3 pr=0 pw=0 time=10 us cost=1 size=11 card=1)
             1          1          1                INDEX UNIQUE SCAN PARTNERS_IX1 (cr=2 pr=0 pw=0 time=6 us cost=0 size=0 card=1)(object id 91563)
           193        193        193              TABLE ACCESS FULL EQUITYKEYS (cr=6143 pr=2555 pw=0 time=4362460 us cost=1303 size=240 card=15)
           193        193        193             TABLE ACCESS BY INDEX ROWID SECURITIES (cr=389 pr=0 pw=0 time=1000 us cost=1 size=44 card=1)
           193        193        193              INDEX UNIQUE SCAN P_SECURITIES (cr=196 pr=0 pw=0 time=498 us cost=0 size=0 card=1)(object id 93819)
           193        193        193            TABLE ACCESS BY INDEX ROWID SECURITIES (cr=389 pr=0 pw=0 time=1486 us cost=2 size=17 card=1)
           193        193        193             INDEX RANGE SCAN SECURITIES_IX2 (cr=197 pr=0 pw=0 time=899 us cost=1 size=0 card=1)(object id 93809)
         24211      24211      24211           TABLE ACCESS BY INDEX ROWID HOLKEYS (cr=12304 pr=44 pw=0 time=38145 us cost=13 size=340 card=20)
         24211      24211      24211            INDEX RANGE SCAN I_HOLKEYS_SECPOR (cr=475 pr=44 pw=0 time=13253 us cost=2 size=0 card=23)(object id 98864)
         11156      11156      11156          TABLE ACCESS BY INDEX ROWID CUSTODIES (cr=4 pr=0 pw=0 time=93056 us cost=1 size=12 card=1)
         48422      48422      48422           INDEX RANGE SCAN R_CUSTODIES_CSTD (cr=3 pr=0 pw=0 time=35831 us cost=0 size=0 card=3)(object id 101437)
         11156      11156      11156         TABLE ACCESS BY INDEX ROWID PORTFOLIOS (cr=13233 pr=0 pw=0 time=31391 us cost=1 size=18 card=1)
         11156      11156      11156          INDEX UNIQUE SCAN P_PORTFOLIOS (cr=2077 pr=0 pw=0 time=12494 us cost=0 size=0 card=1)(object id 91399)
         11156      11156      11156        INDEX UNIQUE SCAN P_PORTGROUPS (cr=9762 pr=0 pw=0 time=15817 us cost=0 size=0 card=1)(object id 95885)
         11156      11156      11156       TABLE ACCESS BY INDEX ROWID PORTGROUPS (cr=11156 pr=0 pw=0 time=15160 us cost=1 size=7 card=1)
             5          5          5      VIEW  index$_join$_005 (cr=6 pr=0 pw=0 time=656 us cost=3 size=45 card=5)
             5          5          5       HASH JOIN  (cr=6 pr=0 pw=0 time=648 us)
             5          5          5        INDEX FAST FULL SCAN PORTGROUPTYPES_IX1 (cr=3 pr=0 pw=0 time=26 us cost=1 size=45 card=5)(object id 95857)
             7          7          7        INDEX FAST FULL SCAN P_PORTGROUPTYPES (cr=3 pr=0 pw=0 time=15 us cost=1 size=45 card=5)(object id 95858)
          2043       2043       2043     TABLE ACCESS BY INDEX ROWID HOLDINGS (cr=90235 pr=2819 pw=0 time=19041512 us cost=38 size=52 card=1)
         92362      92362      92362      INDEX RANGE SCAN R_HOLDINGS_HOLKEYIK (cr=22368 pr=436 pw=0 time=2663719 us cost=2 size=0 card=42)(object id 2173885)
        166827     166827     166827    VIEW PUSHED PREDICATE  (cr=17345612 pr=12466 pw=0 time=107441684 us cost=58 size=36 card=1)
        166827     166827     166827     SORT GROUP BY (cr=17345612 pr=12466 pw=0 time=107408172 us cost=58 size=117 card=1)
       1911217    1911217    1911217      NESTED LOOPS  (cr=17345612 pr=12466 pw=0 time=1296620056 us)
      10575944   10575944   10575944       NESTED LOOPS  (cr=9800492 pr=1140 pw=0 time=22104782 us cost=57 size=117 card=1)
       2292837    2292837    2292837        NESTED LOOPS  (cr=5195338 pr=0 pw=0 time=9516177 us cost=19 size=65 card=1)
          4086       4086       4086         MERGE JOIN CARTESIAN (cr=4885 pr=0 pw=0 time=93451 us cost=6 size=52 card=1)
          4086       4086       4086          NESTED LOOPS  (cr=4103 pr=0 pw=0 time=29074 us cost=4 size=35 card=1)
          2043       2043       2043           NESTED LOOPS  (cr=4099 pr=0 pw=0 time=20146 us cost=3 size=23 card=1)
          2043       2043       2043            TABLE ACCESS BY INDEX ROWID PARTNERS (cr=2052 pr=0 pw=0 time=10371 us cost=2 size=11 card=1)
          2043       2043       2043             INDEX UNIQUE SCAN P_PARTNERS (cr=9 pr=0 pw=0 time=4583 us cost=1 size=0 card=1)(object id 91507)
          2043       2043       2043            TABLE ACCESS BY INDEX ROWID ACCDEF (cr=2047 pr=0 pw=0 time=7133 us cost=1 size=12 card=1)
          2043       2043       2043             INDEX UNIQUE SCAN ACCDEF_IX1 (cr=4 pr=0 pw=0 time=3680 us cost=0 size=0 card=1)(object id 103661)
          4086       4086       4086           TABLE ACCESS BY INDEX ROWID CUSTODIES (cr=4 pr=0 pw=0 time=10204 us cost=1 size=12 card=1)
          4086       4086       4086            INDEX RANGE SCAN R_CUSTODIES_CSTD (cr=3 pr=0 pw=0 time=6320 us cost=0 size=0 card=1)(object id 101437)
          4086       4086       4086          BUFFER SORT (cr=782 pr=0 pw=0 time=60735 us cost=5 size=17 card=1)
          4086       4086       4086           TABLE ACCESS BY INDEX ROWID SECURITIES (cr=782 pr=0 pw=0 time=27892 us cost=2 size=17 card=1)
          4086       4086       4086            INDEX RANGE SCAN SECURITIES_IX2 (cr=590 pr=0 pw=0 time=13746 us cost=1 size=0 card=1)(object id 93809)
       2292837    2292837    2292837         TABLE ACCESS BY INDEX ROWID HOLKEYS (cr=5190453 pr=0 pw=0 time=12329304 us cost=13 size=13 card=1)
      10116948   10116948   10116948          INDEX RANGE SCAN I_HOLKEYS_SECPOR (cr=41387 pr=0 pw=0 time=2136775 us cost=2 size=0 card=23)(object id 98864)
      10575944   10575944   10575944        INDEX RANGE SCAN R_HOLDINGS_HOLKEYIK (cr=4605154 pr=1140 pw=0 time=14152971 us cost=2 size=0 card=42)(object id 2173885)
       1911217    1911217    1911217       TABLE ACCESS BY INDEX ROWID HOLDINGS (cr=7545120 pr=11326 pw=0 time=78999228 us cost=38 size=52 card=1)
    Rows     Execution Plan
          0  SELECT STATEMENT   MODE: ALL_ROWS
       2039   HASH (GROUP BY)
    166827    NESTED LOOPS
       2043     NESTED LOOPS
      11156      HASH JOIN
      11156       VIEW OF 'index$_join$_005' (VIEW)
      11156        HASH JOIN
      11156         INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                        'PORTGROUPTYPES_IX1' (INDEX (UNIQUE))
      11156         INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                        'P_PORTGROUPTYPES' (INDEX (UNIQUE))
      24211       HASH JOIN
        193        VIEW OF 'index$_join$_004' (VIEW)
        193         HASH JOIN
        193          INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                         'P_PORTGROUPS' (INDEX (UNIQUE))
          1          INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                         'R_PORTGROUPS_PORGRPTYPEIK' (INDEX)
          1        HASH JOIN
          1         HASH JOIN
          1          TABLE ACCESS   MODE: ANALYZED (FULL) OF
                         'EQUITYKEYS' (TABLE)
          1          HASH JOIN
        193           TABLE ACCESS   MODE: ANALYZED (FULL) OF
                          'SECURITIES' (TABLE)
        193           HASH JOIN
        193            VIEW OF 'index$_join$_012' (VIEW)
        193             HASH JOIN
        193              INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                             'P_SECURITIES' (INDEX (UNIQUE))
      24211              INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                             'SECURITIES_IX2' (INDEX (UNIQUE))
      24211            NESTED LOOPS
      11156             NESTED LOOPS
      48422              NESTED LOOPS
      11156               NESTED LOOPS
      11156                TABLE ACCESS   MODE: ANALYZED (BY
                               INDEX ROWID) OF 'ACCDEF' (TABLE)
      11156                 INDEX   MODE: ANALYZED (UNIQUE SCAN)
                                OF 'ACCDEF_IX1' (INDEX (UNIQUE))
      11156                TABLE ACCESS   MODE: ANALYZED (BY
                               INDEX ROWID) OF 'PARTNERS' (TABLE)
          5                 INDEX   MODE: ANALYZED (UNIQUE SCAN)
                                OF 'PARTNERS_IX1' (INDEX (UNIQUE))
          5               TABLE ACCESS   MODE: ANALYZED (BY INDEX
                              ROWID) OF 'CUSTODIES' (TABLE)
          5                INDEX   MODE: ANALYZED (RANGE SCAN) OF
                               'R_CUSTODIES_CSTD' (INDEX)
          7              INDEX   MODE: ANALYZED (RANGE SCAN) OF
                             'R_HOLKEYS_CDYIK' (INDEX)
       2043             TABLE ACCESS   MODE: ANALYZED (BY INDEX
                            ROWID) OF 'HOLKEYS' (TABLE)
      92362         VIEW OF 'index$_join$_013' (VIEW)
    166827          HASH JOIN
    166827           INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                          'P_PORTFOLIOS' (INDEX (UNIQUE))
    1911217           INDEX   MODE: ANALYZED (FAST FULL SCAN) OF
                          'PORTFOLIOS_IX1' (INDEX (UNIQUE))
    10575944      TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID) OF
                     'HOLDINGS' (TABLE)
    2292837       INDEX   MODE: ANALYZED (RANGE SCAN) OF
                      'R_HOLDINGS_HOLKEYIK' (INDEX)
       4086     VIEW PUSHED PREDICATE
       4086      SORT (GROUP BY)
       2043       NESTED LOOPS
       2043        NESTED LOOPS
       2043         MERGE JOIN (CARTESIAN)
       2043          NESTED LOOPS
       2043           TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID)
                          OF 'PARTNERS' (TABLE)
       4086            INDEX   MODE: ANALYZED (UNIQUE SCAN) OF
                           'P_PARTNERS' (INDEX (UNIQUE))
       4086           TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID)
                          OF 'CUSTODIES' (TABLE)
       4086            INDEX   MODE: ANALYZED (RANGE SCAN) OF
                           'R_CUSTODIES_CSTD' (INDEX)
       4086          BUFFER (SORT)
       4086           TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID)
                          OF 'SECURITIES' (TABLE)
    2292837            INDEX   MODE: ANALYZED (RANGE SCAN) OF
                           'SECURITIES_IX2' (INDEX (UNIQUE))
    10116948         TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID) OF
                        'HOLKEYS' (TABLE)
    10575944          INDEX   MODE: ANALYZED (RANGE SCAN) OF
                         'I_HOLKEYS_SECPOR' (INDEX)
    1911217        VIEW PUSHED PREDICATE OF 'VW_GBC_22' (VIEW)
          0         SORT (GROUP BY)
          0          NESTED LOOPS
          0           TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID)
                          OF 'ACCDEF' (TABLE)
          0            INDEX   MODE: ANALYZED (UNIQUE SCAN) OF
                           'ACCDEF_IX1' (INDEX (UNIQUE))
          0           TABLE ACCESS   MODE: ANALYZED (BY INDEX ROWID)
                          OF 'HOLDINGS' (TABLE)
          0            INDEX   MODE: ANALYZED (RANGE SCAN) OF
                           'R_HOLDINGS_HOLKEYIK' (INDEX)
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      SQL*Net message to client                       2        0.00          0.00
      db file sequential read                     16176        0.17         90.98
      db file scattered read                        590        0.01          1.94
      Disk file operations I/O                       15        0.00          0.00
      SQL*Net message from client                     2        0.04          0.04
      SQL*Net more data to client                    33        0.00          0.00

    the plans show that your first query has to do much more work than the second query (250M vs. 17M consistent gets). The queries use different plans and the second plan seems to be the better choice - since the first query does not return any rows. The plans also tell us where the time is spent - and there are some nested loops with extreme execution counts and a lot of expansive index access operations (especially the skip scan on P_HOLDINGS), but I would prefer to use a a plan with rowsource statistics to find the errors in the optimizers cardinality estimations.

  • Help needed to tune the Query:Statistics added

    Can someone DBA please help me to tune this query:
    SELECT DISTINCT K.ATTRIBUTE_VALUE AGENCY_ID,B.PROFILE_NM ,NVL(G.OFFICE_DESC,'--') OFFICE_DESC,f.OFFICE_ID,B.PROFILE_ID,'%' ROLE,'%' LAYOUT,
    CASE
    WHEN 'flagB' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING')
    WHEN 'flagO' = '%' THEN
    NVL(J.ISS_GRP_DESC,'ORDERING')
    WHEN 'flag' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING/ORDERING')
    ELSE
    NVL(J.ISS_GRP_DESC,' ')
    END ISS_GRP_DESC,
    DECODE(NVL(H.USERID,' ') ,' ','--','<a sbcuid_in=' || H.USERID || ' target=NEW >'||H.FIRSTNAME || ' ' || H.LASTNAME || '( ' || H.USERID || ' )</a>' ) USER_NAME
    FROM
    PROFILE_PORTAL B ,
    TBL_BDA_AGENCY_RESP_REP C ,
    TBL_BDA_AGENCY_OFFICE F,
    TBL_BDA_OFFICE G,
    USERS_PORTAL H,
    TBL_BDA_USR_ISS_GRP I ,
    TBL_BDA_ISS_GROUP J,
    ATTRIBUTE_VALUES_PORTAL K,
    PROFILE_TYPE_PORTAL L
    WHERE
    B.PROFILE_ID = F.AGENCY_ID (+)
    AND B.PROFILE_ID = C.AGENCY_ID (+)
    AND G.OFFICE_ID (+)= F.OFFICE_ID
    AND H.USERID (+)= C.RESP_USR_ID
    AND C.ISS_GRP_ID = I.ISS_GRP_ID (+)
    AND I.ISS_GRP_ID = J.ISS_GRP_ID(+)
    AND 'PROFILE.'||B.PROFILE_ID = K.ENTITY_ID(+)
    AND K.ATTRIBUTE_VALUE IS NOT NULL
    AND L.PROFILE_TYPE_ID = B.PROFILE_TYPE_ID
    AND L.APPLICATION_CD='BDA'
    AND NOT EXISTS (SELECT agency_id
    FROM TBL_BDA_AGENCY_RESP_REP t
    WHERE t.ISS_GRP_ID IN ('%')
    AND t.AGENCY_ID = C.AGENCY_ID)
    AND K.ATTRIBUTE_VALUE LIKE '%'
    AND UPPER(B.PROFILE_NM) LIKE UPPER('%')
    AND (to_char(NVL(B.PROFILE_ID,0)) LIKE '%' OR NVL(B.PROFILE_ID,0) IN ('a'))
    AND NVL(G.OFFICE_ID,0) IN ('%')
    AND (to_char(NVL(C.RESP_USR_ID,'0')) LIKE '%' OR NVL(C.RESP_USR_ID,'0') IN ('k'))
    ORDER BY PROFILE_NM
    The number of rows in these tables are as follows:
    PROFILE_PORTAL -- 2392
    TBL_BDA_AGENCY_RESP_REP 3508
    TBL_BDA_AGENCY_OFFICE 2151
    TBL_BDA_OFFICE 3
    USERS_PORTAL 270500
    TBL_BDA_USR_ISS_GRP 234
    TBL_BDA_ISS_GROUP 2
    ATTRIBUTE_VALUES_PORTAL 2790
    PROFILE_TYPE_PORTAL 3
    The Explain pal nhas given this o/p to me:
    SQL> select * from table(dbms_xplan.display) dual;
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes |TempSpc| Cost |
    | 0 | SELECT STATEMENT | | 807 | 102K| | 2533 |
    | 1 | SORT UNIQUE | | 807 | 102K| 232K| 82 |
    |* 2 | FILTER | | | | | |
    |* 3 | HASH JOIN OUTER | | 807 | 102K| | 52 |
    |* 4 | HASH JOIN OUTER | | 807 | 95226 | | 40 |
    |* 5 | TABLE ACCESS BY INDEX ROWID | ATTRIBUTE_VALUES | 1 | 23 | | 2 |
    | 6 | NESTED LOOPS | | 7 | 805 | | 37 |
    | 7 | NESTED LOOPS OUTER | | 6 | 552 | | 25 |
    |* 8 | FILTER | | | | | |
    | 9 | NESTED LOOPS OUTER | | | | | |
    |* 10 | FILTER | | | | | |
    | 11 | NESTED LOOPS OUTER | | | | | |
    | 12 | NESTED LOOPS OUTER | | 3 | 141 | | 10 |
    |* 13 | HASH JOIN | | 3 | 120 | | 7 |
    |* 14 | TABLE ACCESS FULL | PROFILE | 6 | 198 | | 4 |
    |* 15 | TABLE ACCESS FULL | PROFILE_TYPE | 1 | 7 | | 2 |
    |* 16 | INDEX RANGE SCAN | SYS_C0019777 | 1 | 7 | | 1 |
    | 17 | TABLE ACCESS BY INDEX ROWID| TBL_BDA_OFFICE | 1 | 10 | | 1 |
    |* 18 | INDEX UNIQUE SCAN | SYS_C0019800 | 1 | | | |
    | 19 | TABLE ACCESS BY INDEX ROWID | TBL_BDA_AGENCY_RESP_REP | 2 | 26 | | 2 |
    |* 20 | INDEX RANGE SCAN | IDX_AGECYRESP_AGNCYID | 2 | | | 1 |
    | 21 | TABLE ACCESS BY INDEX ROWID | USER_ | 1 | 22 | | 1 |
    |* 22 | INDEX UNIQUE SCAN | USER_PK | 1 | | | |
    |* 23 | INDEX RANGE SCAN | IDX_ATTVAL_ENTATTID | 1 | | | 1 |
    | 24 | TABLE ACCESS FULL | TBL_BDA_USR_ISS_GRP | 234 | 702 | | 2 |
    | 25 | TABLE ACCESS FULL | TBL_BDA_ISS_GROUP | 2 | 24 | | 2 |
    |* 26 | TABLE ACCESS BY INDEX ROWID | TBL_BDA_AGENCY_RESP_REP | 1 | 7 | | 3 |
    |* 27 | INDEX RANGE SCAN | IDX_AGECYRESP_AGNCYID | 2 | | | 1 |
    Predicate Information (identified by operation id):
    2 - filter( NOT EXISTS (SELECT /*+ */ 0 FROM "TBL_BDA_AGENCY_RESP_REP" "T" WHERE "T"."AGENCY_ID"=:B1
    AND "T"."ISS_GRP_ID"=TO_NUMBER('%')))
    3 - access("I"."ISS_GRP_ID"="J"."ISS_GRP_ID"(+))
    4 - access("SYS_ALIAS_1"."ISS_GRP_ID"="I"."ISS_GRP_ID"(+))
    5 - filter("K"."ATTRIBUTE_VALUE" IS NOT NULL AND "K"."ATTRIBUTE_VALUE" LIKE '%')
    8 - filter(NVL("SYS_ALIAS_1"."RESP_USR_ID",'0') LIKE '%' OR NVL("SYS_ALIAS_1"."RESP_USR_ID",'0')='k')
    10 - filter(NVL("G"."OFFICE_ID",0)=TO_NUMBER('%'))
    13 - access("L"."PROFILE_TYPE_ID"="B"."PROFILE_TYPE_ID")
    14 - filter(UPPER("B"."PROFILE_NM") LIKE '%' AND (TO_CHAR(NVL("B"."PROFILE_ID",0)) LIKE '%' OR
    NVL("B"."PROFILE_ID",0)=TO_NUMBER('a')))
    15 - filter("L"."APPLICATION_CD"='BDA')
    16 - access("B"."PROFILE_ID"="F"."AGENCY_ID"(+))
    18 - access("G"."OFFICE_ID"(+)="F"."OFFICE_ID")
    20 - access("B"."PROFILE_ID"="SYS_ALIAS_1"."AGENCY_ID"(+))
    22 - access("H"."USERID"(+)="SYS_ALIAS_1"."RESP_USR_ID")
    23 - access("K"."ENTITY_ID"='PROFILE.'||TO_CHAR("B"."PROFILE_ID"))
    26 - filter("T"."ISS_GRP_ID"=TO_NUMBER('%'))
    27 - access("T"."AGENCY_ID"=:B1)
    Note: cpu costing is off
    57 rows selected.
    Elapsed: 00:00:01.08
    Please help me.
    Aashish S.

    Hello Eric,
    Here is the code:
    SELECT DISTINCT
    K.ATTRIBUTE_VALUE AGENCY_ID,
    B.PROFILE_NM ,
    NVL(G.OFFICE_DESC,'--') OFFICE_DESC,
    f.OFFICE_ID,
    B.PROFILE_ID,
    '%' ROLE,
    '%' LAYOUT,
    case
    WHEN 'flagB' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING')
    WHEN 'flagO' = '%' THEN
    NVL(J.ISS_GRP_DESC,'ORDERING')
    WHEN 'flag' = '%' THEN
    NVL(J.ISS_GRP_DESC,'BILLING/ORDERING')
    else
    NVL(J.ISS_GRP_DESC,' ')
    END ISS_GRP_DESC,
    DECODE(NVL(H.USERID,' ') ,' ','--','&lt;a sbcuid_in=' || H.USERID || ' target=NEW &gt;'||H.FIRSTNAME || ' ' || H.LASTNAME ||
    '( ' || H.USERID || ' )&lt;/a&gt;' ) USER_NAME
    from
    PROFILE_PORTAL B ,
    TBL_BDA_AGENCY_RESP_REP C ,
    TBL_BDA_AGENCY_OFFICE F,
    TBL_BDA_OFFICE G,
    USERS_PORTAL H,
    TBL_BDA_USR_ISS_GRP I ,
    TBL_BDA_ISS_GROUP J,
    ATTRIBUTE_VALUES_PORTAL K,
    PROFILE_TYPE_PORTAL L
    WHERE
    B.PROFILE_ID = F.AGENCY_ID (+)
    AND B.PROFILE_ID = C.AGENCY_ID (+)
    AND G.OFFICE_ID (+)= F.OFFICE_ID
    AND H.USERID (+)= C.RESP_USR_ID
    AND C.ISS_GRP_ID = I.ISS_GRP_ID (+)
    AND I.ISS_GRP_ID = J.ISS_GRP_ID(+)
    AND 'PROFILE.'||B.PROFILE_ID = K.ENTITY_ID(+)
    AND K.ATTRIBUTE_VALUE IS NOT NULL
    AND L.PROFILE_TYPE_ID = B.PROFILE_TYPE_ID
    AND L.APPLICATION_CD='BDA'
    AND NOT EXISTS
    (SELECT agency_id
    FROM TBL_BDA_AGENCY_RESP_REP t
    WHERE t.ISS_GRP_ID IN (1)
    AND t.AGENCY_ID = C.AGENCY_ID)
    AND K.ATTRIBUTE_VALUE LIKE '%'
    AND UPPER(B.PROFILE_NM) LIKE UPPER('%')
    AND (to_char(NVL(B.PROFILE_ID,0))
    LIKE '%'
    OR NVL(B.PROFILE_ID,0) IN (1))
    AND NVL(G.OFFICE_ID,0) IN (1)
    AND (to_char(NVL(C.RESP_USR_ID,'0'))
    LIKE '%'
    OR NVL(C.RESP_USR_ID,'0') IN ('%'))
    ORDER BY PROFILE_NM
    This is the Query and the query takes some mins. to run in prod environment.
    From the Query plan ,I am not able to get any idea for optimization.
    Now,Can you tell me which steps I need to follow to run it faster and which all modifications should be made?
    Thanks.
    Aashish S.

  • Query help in select Distinct on one column.

    CREATE GLOBAL TEMPORARY TABLE Table1 (
    ID1 varchar2(100) ,
    Name1 varchar2(100),
    Name11 varchar2(100)
    insert into Table1 values ('a','n1','h3');
    insert into Table1 values('b','n2','h2');
    insert into Table1 values('a','n3','h1');
    insert into Table1 values('c','n4','h5');
    insert into Table1 values ('c','n5','h4');
    insert into Table1 values('d','n6','h6');
    select * from Table1;
    ID1,NAME1,     NAME11
    a,     n1,     h3
    b,     n2,     h2
    a,     n3,     h1
    c,     n4,     h5
    c,     n5,     h4
    d,     n6,     h6
    I am trying to select distinct ID1 and all values associated with it which is max row.I want to result as -
    ID1,NAME1,     NAME11
    a,     n3,     h1
    b,     n2,     h2
    c,     n5,     h4
    d,     n6,     h6
    Can you please help me to write simple query to get above result.
    Edited by: 871447 on Jul 25, 2011 9:42 AM
    Edited by: 871447 on Jul 25, 2011 9:45 AM

    Hi,
    Do a self-join, to combine the two rows for each value of id1 onto one output row.
    Make it an outer join, in case there is only one row with a vlaue for id1.
    SELECT  l.id1
    ,     l.name1
    ,     NVL ( r.name11
             , l.name11
             )          AS name11
    FROM              table1     l
    LEFT OUTER JOIN     table1     r  ON  l.id1     = r.id1
                              AND l.name1     < r.name1
    ;Edited by: Frank Kulash on Jul 25, 2011 12:57 PM
    Sorry, I mis-read the problem.
    Lee's solution, above, assumes that name1 is unique, as it is in your sample data.
    What output would you want if that's not the case?
    If name1 is not unique, but the combination of (id1, name1) is unique, then you can modify Lee's solution like this:
    SELECT  *
    FROM    table1
    WHERE   (id1, name1) IN (
                        SELECT    id1
                        ,       MAX (name1)
                               FROM          table1
                        GROUP BY  id1
    ;Or, if you can't make any assumptions about uniqueness, you might need something like this:
    WITH     got_r_num     AS
         SELECT  id1, name1, name11
         ,     ROW_NUMBER () OVER ( PARTITION BY  id1
                                   ORDER BY          name1     DESC
                             ,                name11     DESC
                           )      AS r_num
         FROM     table1
    SELECT  id1, name1, name11
    FROM     got_r_num
    WHERE     r_num     = 1
    ;

  • Help needed for implementing master details region on the search page

    Hi All,
         I am new to Jdeveloper and OAF. I need to develop a sales order search page, for which the VO query is
         SELECT ch.order_number order_number
         , ch.attribute97 order_detail
         , ch.attribute14 sold_to
         , ch.attribute50 business_group
         , ch.source_doc_type source_doc_type
         , ch.line_number line_number
         , ch.transaction_amount transaction_amount
         , ch.split_pct||'%' split_percentage
         , jrs.NAME resource_name
         , jrr.resource_number resource_number
         , ch.processed_date processed_date
         , cq.NAME quota_name
         , ch.attribute34 transaction_type
         , ch.attribute35 order_type
         , ch.attribute51 product_line
         , ch.attribute54 city
         , ch.attribute55 state
         , ch.attribute56 district
         , ch.attribute57 area
         , ch.attribute61 salesrep_number
         , ch.attribute62 region
         , ch.attribute64 sales_org
         , ch.attribute68 terr_order_type
         , ch.attribute58 market_indicator
         FROM cn_commission_lines_all cl
         , cn_commission_headers_all ch
         , cn_quotas_all cq
         , jtf_rs_salesreps jrs
         , jtf_rs_resource_extns jrr
         WHERE cl.direct_salesrep_id = cl.credited_salesrep_id
         AND cl.commission_header_id = ch.commission_header_id
         AND cl.quota_id = cq.quota_id
         AND ch.direct_salesrep_id = jrs.salesrep_id
         AND jrr.resource_id = jrs.resource_id
         AND (NVL(ch.source_doc_type,'X') = 'EVEREST' OR NVL(ch.source_doc_type,'X') = 'SAP')
    AND (NVL(ch.attribute50,'X') = 'EMG' OR NVL(ch.attribute50,'X') = 'LSCA')
    I have 3 query parameters business_group, source_doc_type and sales order number.
    The results region should look like,
    Header region with order_number,order_detail,sold_to,business_group and the source_doc_type columns.
    The remaining fields go into the details region.
    Highly appreciate if anyone could help me on this.
    Regards,

    Hi,
    When I have a messagestyle layout for my header, I am not able to find the items in this region for the querycriteriamap. I need to populate the header region with values entered in the search region.
    For example: I create the OrderNumber messagestyledtext item in the messagecomponent layout of the header region. In simplesearchmappings, for the querycriteriamap, I am not able to find this item in the drop down for Results Item property.
    Please help me to understand.
    Regards,

  • Use of NVL, Is it correct or wrong

    Hi,
    I've doubt regarding the use of NVL in SQL query,
    Eg.
    for query :
    SELECT *
    FROM NBS_POLICY_MST
    WHERE ORG_POLICY_ID = NVL(:p1,ORG_POLICY_ID);
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
    | 0 | SELECT STATEMENT | | 2084 | 439K| 33 (4)|
    | 1 | CONCATENATION | | | | |
    | 2 | FILTER | | | | |
    | 3 | TABLE ACCESS FULL | NBS_POLICY_MST | 2083 | 439K| 30 (4)|
    | 4 | FILTER | | | | |
    | 5 | TABLE ACCESS BY INDEX ROWID| NBS_POLICY_MST | 1 | 216 | 3 (0)|
    | 6 | INDEX RANGE SCAN | IDXPT_NMP_TRAIL | 1 | | 2 (0)|
    In above plan there is an FTS
    but if I convert the query to
    SELECT *
    FROM NBS_POLICY_MST
    WHERE POLICY_ID = :p1 or :p1 is null;
    PLAN_TABLE_OUTPUT
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
    | 0 | SELECT STATEMENT | | 105 | 22680 | 29 (0)|
    | 1 | TABLE ACCESS FULL| NBS_POLICY_MST | 105 | 22680 | 29 (0)|
    the plan table output cost decreased little.
    Can anyone help me in the above query which one is the best practice and why and
    for the other one why it is consuming more memory
    If possible provide a comprehensive description for my above doubt,
    thanks in advance
    regards/ goutam

    SELECT  *
    FROM    NBS_POLICY_MST
    WHERE   ORG_POLICY_ID   = NVL(:p1,ORG_POLICY_ID);
    PLAN_TABLE_OUTPUT
    | Id  | Operation                     | Name            | Rows  | Bytes | Cost (%CPU)|
    |   0 | SELECT STATEMENT              |                 |  2084 |   439K|    33   (4)|
    |   1 |  CONCATENATION                |                 |       |       |            |
    |   2 |   FILTER                      |                 |       |       |            |
    |   3 |    TABLE ACCESS FULL          | NBS_POLICY_MST  |  2083 |   439K|    30   (4)|
    |   4 |   FILTER                      |                 |       |       |            |
    |   5 |    TABLE ACCESS BY INDEX ROWID| NBS_POLICY_MST  |     1 |   216 |     3   (0)|
    |   6 |     INDEX RANGE SCAN          | IDXPT_NMP_TRAIL |     1 |       |     2   (0)|
    --------------------------------------------------------------------------------------here the problem comes when org_policy_id is a nullable field. what if both org_policy_id is null and : p is null. It will not be matched.
    Hence i guess oracle does perdicate like this.
    (:p is null and org_policy_id is not null) or (:p is not null and org_policy_id = :p)
    SELECT  *
    FROM    NBS_POLICY_MST
    WHERE   POLICY_ID   = :p1 or :p1 is null;
    PLAN_TABLE_OUTPUT
    | Id  | Operation         | Name           | Rows  | Bytes | Cost (%CPU)|
    |   0 | SELECT STATEMENT  |                |   105 | 22680 |    29   (0)|
    |   1 |  TABLE ACCESS FULL| NBS_POLICY_MST |   105 | 22680 |    29   (0)|
    -------------------------------------------------------------------------I guess this is the correct way to do it and not NVL.

  • Need help in forming a query

    Hi All,
    I have a table with the following data:
    col1 co2 col3
    1 A 5
    2 B 3
    3 C 3
    4 A 2
    5 A 5
    6 D 2
    7 C 1
    8 C 6
    9 A 7
    I will explain my requirement by considering above data.
    1) I want to get the sum of col3 values.
    2) I should consider only the rows which are in between "A" and "C" - (here A & C are referring to col2 values).
    (i.e,
    1 A 5
    2 B 3
    3 C 3
    5 A 5
    6 D 2
    7 C 1
    ) - If there are multiple A-C combination then consider only the innermost.
    3) from the above set i should get the sum of col3 values by considering only the values of last A-C combination rows.
    (i.e,
    5 A 5
    6 D 2
    7 C 1
    final sum is: *5+2+1 = 8*
    result is 8
    If no such A-C combination is present then i should get "0" as the sum.
    Please help me..
    I have Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
    Thanks,
    Girish G
    Edited by: Girish G on Aug 2, 2011 1:56 AM

    Oh, and to cater for there being no A to C in the data returning 0, just wrap the result in an NVL statement...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as col1, 'A' as col2, 5 as col3 from dual union all
      2             select 2, 'B', 3 from dual union all
      3             select 3, 'D', 3 from dual union all
      4             select 4, 'A', 2 from dual union all
      5             select 5, 'A', 5 from dual union all
      6             select 6, 'D', 2 from dual union all
      7             select 7, 'F', 1 from dual union all
      8             select 8, 'G', 6 from dual union all
      9             select 9, 'A', 7 from dual)
    10  --
    11  -- end of test data
    12  --
    13      ,a as (select max(col1) col1
    14             from t
    15             where col2 = 'A'
    16             and exists (select 1
    17                         from t t2
    18                         where col2 = 'C'
    19                         and   col1 > t.col1)
    20            )
    21      ,c as (select min(t.col1) as col1
    22             from t, a
    23             where t.col1 > a.col1
    24             and   t.col2 = 'C'
    25            )
    26  select NVL(sum(t.col3),0) as col3_sum
    27  from   t, a, c
    28* where t.col1 between a.col1 and c.col1
    SQL> /
      COL3_SUM
             0

Maybe you are looking for

  • I sold my Imac and would like to wipe it out but keep the major programs is that possible?

    I sold my Imac and I would like to wipe it out passwords and all but keep the programs that have been purchased, is that possible and how do you do it?

  • Installing ODI 10.1.3.5  on DB 11g1: error out

    Hi, Got error: Exception java.lang.NoClassDefFoundError: oracle/sysman/oii/oiix/OiixFolderOps occurred.. java.lang.NoClassDefFoundError: oracle/sysman/oii/oiix/OiixFolderOps      at ssCreateItemw32.installAction(ssCreateItemw32.java:379)      at orac

  • Settings in BW system for data to b sent from Xi

    hey guys.. i hav an absolute fresh BW system n my Xi system (not a fresh one) need to knw, wot are the settings i need to make on the BW and on the Xi system for the interfaces (i/b n o/b) which i create in Xi system to b reflected in the BW system w

  • Can iPhone still be identified after remote wipe?

    Got my iPhone stolen. The person turned it off right away. So far it has been offline. I have been sending messages and locking commends through iCloud.com, but have not chosen the remote wipe option yet. Is it possible for a stolen iPhone be identif

  • I can't update my version 10.6.8 to 10.7.2 or later

    In my app store on version 10.6.8 Mac OS X  I am trying to get apps but when I click 'install' it says I need Mac OS X 10.7.2 and I tried to update it in the apple symbol drop down box but it said all my software updates are up to date. Plz help me.