Help tuning this query

The tables are very large...
select a.number
from transaction a,
contr c,
directory b
where EXISTS ( SELECT 1
FROM contr D
WHERE D.CSSTATCHNG LIKE '%a'
AND D.SNCODE IN (3386, 3387)
AND D.COID = C.COID)
AND C.CSSTATCHNG LIKE '%a'
AND b.dnid = c.dnid
and a.snumber = b.dnnum
AND a.CDATE = SYSDATE - 90
AND ROWNUM <= 10
I only need 10 rows.
Note: I use Oracle 8.1.7
Thanks!

You really should analyze, even if you use a fairly small sample percent, it is usually better than no analyzing.
If the tables are large, you may want to try rephrasing the EXISTS as an IN, something like:
SELECT a.number
FROM transaction a, contr c, directory b
WHERE b.dnid = c.dnid and
      a.snumber = b.dnnum AND
      c.coid IN (SELECT coid
                 FROM contr d
                 WHERE d.csstatchng LIKE '%a' AND
                       d.sncode IN (3386, 3387)) AND
      c.csstatchng LIKE '%a' AND
      a.CDATE = SYSDATE - 90 AND
      ROWNUM <= 10or perhaps as a join to an in-line view like:
SELECT a.number
FROM transaction a, contr c, directory b,
     (SELECT coid FROM contr
      WHERE csstatchng LIKE '%a' AND
            sncode IN (3386, 3387)) d
WHERE b.dnid = c.dnid and
      a.snumber = b.dnnum AND
      c.coid = d.coid AND
      c.csstatchng LIKE '%a' AND
      a.CDATE = SYSDATE - 90 AND
      ROWNUM <= 10Although, I can't help feeling that you cuold skip that bit altogether and simply do:
SELECT a.number
FROM transaction a, contr c, directory b
WHERE b.dnid = c.dnid and
      a.snumber = b.dnnum AND
      c.sncode IN (3386, 3387) AND
      c.csstatchng LIKE '%a' AND
      a.CDATE = SYSDATE - 90 AND
      ROWNUM <= 10HTH
John

Similar Messages

  • Please can you help me in Tuning this query..?

    Hi ,
    Please can you help me in re-structuring this query? .Details are given below.
    I have 2 tables as shown below and data is like this.
    Position
    COD IND
    AAA N
    BBB N
    CCC N
    DDD Y
    Distance
    orig dest
    AAA BBB
    BBB CCC
    AAA CCC
    I need to create the records like this
    start end
    DDD AAA
    DDD BBB
    DDD CCC
    The query which i am using now for this is
    select p.code AS start,
    P1.CODE AS end
    from position p, position p1
    where
    P.CODE != P1.CODE
    AND (P.ind = 'Y' or P1.IND = 'Y')
    AND not exists
    (select 1
    from distance d
    where (d.orig = p.code or d.dest = p.code)
    and (d.orig = p1.code or d.dest = p1.code))
    table is having above a crore record. so its taking a lot of time.
    Please someone please help in tuning this query?
    Thanks and regards,
    Shabir

    Looks like you want this
    select a.strt, b.ends from
    (select p.code strt from position p where p.ind='Y') a,
    (select p.code ends from position p where p.ind='N') b
    where not exists (select 1 from distance d where d.orig=a.strt or d.dest=a.strt);
    DDD     AAA
    DDD     BBB
    DDD     CCCYour query result is:
    AAA     DDD
    BBB     DDD
    CCC     DDD
    DDD     AAA
    DDD     BBB
    DDD     CCCYou should be more descriptive about what kind of result you want, so that people can get more interested in helping you.

  • Tuning this query

    Hi,
    Can anybody please help me in tuning this query?
    update tablec c set c.col1 = (select b.col1 from tableb b where c.col2 = b.col2 ) where
    c.col2 in (select distinct a.col1 from tablec a, tableb b where a.col2 = b.col2 and a.col1 != b.col1 )
    When i use "alter session force parallel DML" before running this query it takes only seconds, but when i use parallel hint or run it without hint, its running for hours.
    Thanks for your Help

    Hi itssan,
    You could try EXISTS.
    I think this is the same:
    update tablec c
       set c.col1 =
              (select b.col1
                 from tableb b
                where b.col2 = c.col2)
    where exists (select null
                     from tablec a, tableb b
                    where a.col2 = b.col2
                      and a.col1 != b.col1
                      and a.col1 = c.col2);Regards
    Peter

  • Please need help with this query

    Hi !
    Please need help with this query:
    Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
    Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
    select distinct a.id,
    create_date,
    a.loanid,
    a.rate,
    a.pays,
    a.gracetime,
    a.emailtosend,
    d.first_name,
    d.last_name,
    a.user_id
    from CLAL_LOANCALC_DET a,
    loan_Calculator b,
    bv_user_profile c,
    bv_mr_user_profile d
    where b.loanid = a.loanid
    and c.NET_USER_NO = a.resp_id
    and d.user_id = c.user_id
    and a.is_partner is null
    and a.create_date between
    TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
    TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    order by a.create_date

    Perhaps something like this...
    select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
    from (
          select distinct a.id,
                          create_date,
                          a.loanid,
                          a.rate,
                          a.pays,
                          a.gracetime,
                          a.emailtosend,
                          d.first_name,
                          d.last_name,
                          a.user_id,
                          max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
          from CLAL_LOANCALC_DET a,
               loan_Calculator b,
               bv_user_profile c,
               bv_mr_user_profile d
          where b.loanid = a.loanid
          and   c.NET_USER_NO = a.resp_id
          and   d.user_id = c.user_id
          and   a.is_partner is null
          and   a.create_date between
                TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
                TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
    where create_date = max_create_date
    order by create_date

  • Help on this query

    Hi , i have a table of 3000000 rows, and am executing this script
    update products set cas_num = trim(cas_num
    due to which it is taking 99.2% of UNDO TS ,
    I was just exactly thinking the same if i can put it in a simple SQL loop statement and have it commit for like every 10,000 rows.
    Could you please help me a better way to to accomplish this simple SQL loop statement?
    Thanks
    Vk

    Check your original post. You wouldn't get different replies as the same set of people check here too.
    Help on this query ( taking 99% of UNDO TS on 10g )

  • Please help me this query

    Plz help me this query:
    Create a query that displays the employees last name and indicates the amounts of their annual salaries with *. Each * signifies a thousand $'s.
    EG. If the salary of an employee with last name king is 2000 then it should appear as follows:
    King **
    2 (*) for 2 thousand.
    Thanks in advance

    Hi,
    I was able to solve this query on my own. For those who might face a problem in future the solutionto it is:
    SELECT rpad(last_name, length(last_name) + (salary/1000), '*') AS employee_and_their_salaries
    FROM employees;
    Thanks

  • Can someone please suggest me in tuning this query?

    Can someone please suggest me in tuning this query?
    select pemail.email_oid,
    pemail.parent_oid,
    md.message_data_oid,
    ea.email_account_oid,
    ef.email_folder_oid,
    md.channel,
    ea.email_account_name,
    md.from_address,
    md.to_address,
    eref.ref_id ticket_number,
    ( select count(email_oid) from esg.email where email_oid = pemail.email_oid and email.attachment_flag = 'Y' ) attachment_flag_a,
    ( select count(email_oid) from esg.email cemail
    where cemail.parent_oid = pemail.email_oid and
    ( cemail.email_type is null or cemail.email_type != 'S') and
    cemail.vec_status not in ('G', 'D', 'P') and
    cemail.vec_status in ('N', 'O') and
    cemail.attachment_flag = 'Y' ) attachment_flag_b,
    md.received_date,
    pemail.vec_status,
    pemail.email_type,
    md.subject,
    md.content,
    pemail.tracking_id,
    pemail.assignee_oid,
    ( select count(email_oid) from email cemail
    where cemail.parent_oid = pemail.email_oid and
    (cemail.email_type is null or cemail.email_type != 'S' ) and
    cemail.vec_status not in ('G','D', 'P') and
    cemail.vec_status in ('N', 'O' ) ) child_email_cnt,
    ( select count(0) from email
    where vec_status = 'N' and
    email_type = 'O' and
    email_oid = pemail.email_oid and
    vec_status in ('N', 'O' )) parent_newemail_cnt,
    ( select count(0) from email
    where vec_status = 'N' and
    email_type = 'O' and
    parent_oid = pemail.email_oid and
    vec_status in ('N', 'O' )) child_newemail_cnt
    From esg.email pemail,
    esg.message_data md,
    esg.email_account ea,
    esg.email_folder ef,
    (select email_oid, ref_id from email_ref
    where email_ref_oid in (select min(email_ref_oid) from email_ref where ref_tid = -9000 and ref_sid = 'ESG' and confirmed_flag = 'Y' group by email_oid )) eref
    where
    md.message_data_oid = pemail.message_data_oid and
    pemail.email_folder_oid = ef.email_folder_oid(+) and
    ef.email_account_oid = ea.email_account_oid(+) and
    pemail.vec_status not in ('G','D','P') and
    pemail.vec_status in ('N', 'O') and
    pemail.email_oid = eref.email_oid(+) and
    lower(md.channel) in ('[email protected]', '[email protected]') and
    pemail.email_oid in
    ( select parent.email_oid from message_data md,
    email parent
    where md.message_data_oid = parent.message_data_oid and
    parent.parent_oid is null and
    pemail.email_type = 'O' and
    lower(md.to_address) in
    ( select lower(contact_info) from esg.user_profile up,
    esg.person p,
    esg.person_contact pc
    where up.user_oid = 802239 and
    up.person_oid = p.person_oid and
    p.person_oid = pc.person_oid and
    pc.contact_type_oid = 6 )
    union all
    select child.parent_oid from message_data md,
    email child
    where md.message_data_oid = child.message_data_oid and
    parent_oid is not null and
    child.vec_status not in ('G','D','P') and
    child.vec_status in ('N', 'O') and
    child.email_type = 'O' and
    lower(md.to_address) in
    ( select lower(contact_info) from esg.user_profile up,
    esg.person p,
    esg.person_contact pc
    where up.user_oid = 802239 and
    up.person_oid = p.person_oid and
    p.person_oid = pc.person_oid and
    pc.contact_type_oid = 6 )
    ORDER BY RECEIVED_DATE DESC;
    1st run
    Elapsed: 00:00:03.39
    2nd run
    Elapsed: 00:00:01.80
    SQL> Select TABLE_NAME,LAST_ANALYZED, num_rows from user_tables where table_name in ('EMAIL','MESSAGE_DATA', 'EMAIL_ACCOUNT','EMAIL_FOLDER','EMAIL_REF','USER_PROFILE','PERSON','PERSON_CONTACT');
    TABLE_NAME LAST_ANAL NUM_ROWS
    USER_PROFILE 11-JUN-11 385808
    PERSON_CONTACT 11-JUN-11 698624
    PERSON 11-JUN-11 405364
    MESSAGE_DATA 11-JUN-11 1069
    EMAIL_REF 11-JUN-11 559
    EMAIL_FOLDER 11-JUN-11 563
    EMAIL_ACCOUNT 11-JUN-11 563
    EMAIL 11-JUN-11 101652
    8 rows selected.
    row count from all tables
    email - 102063
    message_data - 1069
    email_account - 563
    email_folder - 563
    email_ref - 559
    user_profile - 386055
    person - 404057
    person_contact - 698696
    Thanks,
    Suman M.

    ======================================
    Execution Plan
    Plan hash value: 110416976
    | Id | Operation | Name | Rows | Bytes | Cost |
    | 0 | SELECT STATEMENT | | 1 | 1361 | 17 |
    | 1 | SORT AGGREGATE | | 1 | 8 | |
    |* 2 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 8 | 1 |
    |* 3 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
    | 4 | SORT AGGREGATE | | 1 | 8 | |
    |* 5 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 8 | 1 |
    |* 6 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
    | 7 | SORT AGGREGATE | | 1 | 6 | |
    |* 8 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 6 | 1 |
    |* 9 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
    | 10 | SORT AGGREGATE | | 1 | 10 | |
    |* 11 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 10 | 1 |
    |* 12 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
    | 13 | SORT AGGREGATE | | 1 | 6 | |
    |* 14 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 6 | 1 |
    |* 15 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
    | 16 | SORT ORDER BY | | 1 | 1361 | 17 |
    |* 17 | FILTER | | | | |
    | 18 | NESTED LOOPS OUTER | | 1 | 1361 | 5 |
    | 19 | NESTED LOOPS OUTER | | 1 | 1336 | 4 |
    | 20 | NESTED LOOPS | | 1 | 1328 | 3 |
    | 21 | NESTED LOOPS OUTER | | 1 | 53 | 2 |
    | 22 | INLIST ITERATOR | | | | |
    | 23 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 35 | 1 |
    |* 24 | INDEX RANGE SCAN | IE_EMAIL | 55 | | 1 |
    | 25 | VIEW PUSHED PREDICATE | | 1 | 18 | 1 |
    | 26 | NESTED LOOPS | | 1 | 36 | 4 |
    | 27 | VIEW | VW_NSO_1 | 11 | 143 | 3 |
    | 28 | SORT GROUP BY | | 11 | 231 | 3 |
    |* 29 | TABLE ACCESS BY INDEX ROWID| EMAIL_REF | 11 | 231 | 3 |
    |* 30 | INDEX FULL SCAN | AK1_EMAIL_REF | 21 | | 3 |
    |* 31 | TABLE ACCESS BY INDEX ROWID | EMAIL_REF | 1 | 23 | 1 |
    |* 32 | INDEX UNIQUE SCAN | PK_EMAIL_REF | 1 | | 1 |
    |* 33 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1 | 1275 | 1 |
    |* 34 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
    | 35 | TABLE ACCESS BY INDEX ROWID | EMAIL_FOLDER | 1 | 8 | 1 |
    |* 36 | INDEX UNIQUE SCAN | PK_EMAIL_FOLDER | 1 | | 1 |
    | 37 | TABLE ACCESS BY INDEX ROWID | EMAIL_ACCOUNT | 1 | 25 | 1 |
    |* 38 | INDEX UNIQUE SCAN | PK_EMAIL_ACCOUNT | 1 | | 1 |
    | 39 | UNION-ALL | | | | |
    | 40 | NESTED LOOPS SEMI | | 1 | 116 | 5 |
    | 41 | NESTED LOOPS | | 1 | 50 | 2 |
    |* 42 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 14 | 1 |
    |* 43 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
    | 44 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1069 | 38484 | 1 |
    |* 45 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
    |* 46 | VIEW | VW_NSO_3 | 1 | 66 | 3 |
    |* 47 | FILTER | | | | |
    | 48 | NESTED LOOPS | | 1 | 45 | 3 |
    | 49 | NESTED LOOPS | | 1 | 15 | 2 |
    | 50 | TABLE ACCESS BY INDEX ROWID | USER_PROFILE | 1 | 10 | 1 |
    |* 51 | INDEX UNIQUE SCAN | PK_USER | 1 | | 1 |
    |* 52 | INDEX UNIQUE SCAN | PK_PERSON | 405K| 1979K| 1 |
    |* 53 | TABLE ACCESS BY INDEX ROWID | PERSON_CONTACT | 1 | 30 | 1 |
    |* 54 | INDEX RANGE SCAN | FK_PERSON_CONTACT_PERSON | 2 | | 1 |
    | 55 | NESTED LOOPS | | 1 | 114 | 5 |
    | 56 | MERGE JOIN CARTESIAN | | 1 | 78 | 4 |
    |* 57 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 12 | 1 |
    |* 58 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 1 | | 1 |
    | 59 | BUFFER SORT | | 1 | 66 | 3 |
    | 60 | VIEW | VW_NSO_2 | 1 | 66 | 3 |
    | 61 | SORT UNIQUE | | 1 | 45 | |
    | 62 | NESTED LOOPS | | 1 | 45 | 3 |
    | 63 | NESTED LOOPS | | 1 | 15 | 2 |
    | 64 | TABLE ACCESS BY INDEX ROWID| USER_PROFILE | 1 | 10 | 1 |
    |* 65 | INDEX UNIQUE SCAN | PK_USER | 1 | | 1 |
    |* 66 | INDEX UNIQUE SCAN | PK_PERSON | 405K| 1979K| 1 |
    |* 67 | TABLE ACCESS BY INDEX ROWID | PERSON_CONTACT | 1 | 30 | 1 |
    |* 68 | INDEX RANGE SCAN | FK_PERSON_CONTACT_PERSON | 2 | | 1 |
    |* 69 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1 | 36 | 1 |
    |* 70 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
    Predicate Information (identified by operation id):
    2 - filter("EMAIL"."ATTACHMENT_FLAG"='Y')
    3 - access("EMAIL_OID"=:B1)
    5 - filter("CEMAIL"."EMAIL_TYPE"<>'S' AND "CEMAIL"."VEC_STATUS"<>'G' AND
    "CEMAIL"."VEC_STATUS"<>'D' AND "CEMAIL"."VEC_STATUS"<>'P' AND ("CEMAIL"."VEC_STATUS"='N'
    OR "CEMAIL"."VEC_STATUS"='O') AND "CEMAIL"."ATTACHMENT_FLAG"='Y')
    6 - access("CEMAIL"."PARENT_OID"=:B1)
    8 - filter("CEMAIL"."EMAIL_TYPE"<>'S' AND "CEMAIL"."VEC_STATUS"<>'G' AND
    "CEMAIL"."VEC_STATUS"<>'D' AND "CEMAIL"."VEC_STATUS"<>'P' AND ("CEMAIL"."VEC_STATUS"='N'
    OR "CEMAIL"."VEC_STATUS"='O'))
    9 - access("CEMAIL"."PARENT_OID"=:B1)
    11 - filter("VEC_STATUS"='N' AND "EMAIL_TYPE"='O')
    12 - access("EMAIL_OID"=:B1)
    14 - filter("VEC_STATUS"='N' AND "EMAIL_TYPE"='O')
    15 - access("PARENT_OID"=:B1)
    17 - filter( EXISTS ( (SELECT "PARENT"."EMAIL_OID" FROM "EMAIL" "PARENT","MESSAGE_DATA"
    "MD", (SELECT LOWER("CONTACT_INFO") "$nso_col_1" FROM "ESG"."PERSON_CONTACT"
    "PC","ESG"."PERSON" "P","ESG"."USER_PROFILE" "UP" WHERE :B1='O' AND
    "UP"."USER_OID"=802239 AND "UP"."PERSON_OID"="P"."PERSON_OID" AND
    "P"."PERSON_OID"="PC"."PERSON_OID" AND "PC"."CONTACT_TYPE_OID"=6)
    "VW_NSO_3" WHERE
    "$nso_col_1"=LOWER("MD"."TO_ADDRESS") AND "MD"."MESSAGE_DATA_OID"="PARENT"."MESSAGE_DATA_O
    ID" AND "PARENT"."EMAIL_OID"=:B2 AND "PARENT"."PARENT_OID" IS NULL
    ) UNION ALL (SELECT
    "CHILD"."PARENT_OID" FROM "EMAIL" "CHILD","MESSAGE_DATA" "MD", (SELECT DISTINCT
    LOWER("CONTACT_INFO") "$nso_col_1" FROM "ESG"."PERSON_CONTACT" "PC","ESG"."PERSON"
    "P","ESG"."USER_PROFILE" "UP" WHERE "UP"."USER_OID"=802239 AND
    "UP"."PERSON_OID"="P"."PERSON_OID" AND "P"."PERSON_OID"="PC"."PERSON_OID" AND
    "PC"."CONTACT_TYPE_OID"=6) "VW_NSO_2" WHERE "MD"."MESSAGE_DATA_OID
    "="CHILD"."MESSAGE_DATA_
    OID" AND "$nso_col_1"=LOWER("MD"."TO_ADDRESS") AND "CHILD"."PARENT
    _OID"=:B3 AND
    "CHILD"."VEC_STATUS"<>'G' AND "CHILD"."VEC_STATUS"<>'D' AND "CHILD
    "."VEC_STATUS"<>'P' AND
    ("CHILD"."VEC_STATUS"='N' OR "CHILD"."VEC_STATUS"='O') AND "CHILD"
    ."EMAIL_TYPE"='O' AND
    "PARENT_OID" IS NOT NULL)))
    24 - access("PEMAIL"."VEC_STATUS"='N' OR "PEMAIL"."VEC_STATUS"='O')
    filter("PEMAIL"."VEC_STATUS"<>'G' AND "PEMAIL"."VEC_STATUS"<>'D' AND
    "PEMAIL"."VEC_STATUS"<>'P')
    29 - filter("CONFIRMED_FLAG"='Y')
    30 - access("REF_SID"='ESG' AND "REF_TID"=(-9000))
    filter("REF_TID"=(-9000) AND "REF_SID"='ESG')
    31 - filter("EMAIL_OID"="PEMAIL"."EMAIL_OID")
    32 - access("EMAIL_REF_OID"="$nso_col_1")
    33 - filter(LOWER("MD"."CHANNEL")='[email protected]' OR
    LOWER("MD"."CHANNEL")='[email protected]')
    34 - access("MD"."MESSAGE_DATA_OID"="PEMAIL"."MESSAGE_DATA_OID")
    36 - access("PEMAIL"."EMAIL_FOLDER_OID"="EF"."EMAIL_FOLDER_OID"(+))
    38 - access("EF"."EMAIL_ACCOUNT_OID"="EA"."EMAIL_ACCOUNT_OID"(+))
    42 - filter("PARENT"."PARENT_OID" IS NULL)
    43 - access("PARENT"."EMAIL_OID"=:B1)
    45 - access("MD"."MESSAGE_DATA_OID"="PARENT"."MESSAGE_DATA_OID")
    46 - filter("$nso_col_1"=LOWER("MD"."TO_ADDRESS"))
    47 - filter(:B1='O')
    51 - access("UP"."USER_OID"=802239)
    52 - access("UP"."PERSON_OID"="P"."PERSON_OID")
    53 - filter("PC"."CONTACT_TYPE_OID"=6)
    54 - access("P"."PERSON_OID"="PC"."PERSON_OID")
    57 - filter("CHILD"."VEC_STATUS"<>'G' AND "CHILD"."VEC_STATUS"<>'D' AND
    "CHILD"."VEC_STATUS"<>'P' AND ("CHILD"."VEC_STATUS"='N' OR "CHILD"
    ."VEC_STATUS"='O') AND
    "CHILD"."EMAIL_TYPE"='O')
    58 - access("CHILD"."PARENT_OID"=:B1)
    filter("PARENT_OID" IS NOT NULL)
    65 - access("UP"."USER_OID"=802239)
    66 - access("UP"."PERSON_OID"="P"."PERSON_OID")
    67 - filter("PC"."CONTACT_TYPE_OID"=6)
    68 - access("P"."PERSON_OID"="PC"."PERSON_OID")
    69 - filter("$nso_col_1"=LOWER("MD"."TO_ADDRESS"))
    70 - access("MD"."MESSAGE_DATA_OID"="CHILD"."MESSAGE_DATA_OID")
    Note
    - cpu costing is off (consider enabling it)
    Statistics
    3103 recursive calls
    0 db block gets
    599654 consistent gets
    2915 physical reads
    0 redo size
    1955 bytes sent via SQL*Net to client
    3895 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    331 sorts (memory)
    0 sorts (disk)
    0 rows processed
    =====================================

  • Help in Tuning this Query

    Hi,
    I have a query in my proj where the same table is looked up twice in the same query.
    Can anybody suggest in improving the performance of this query?
    select * from table1 a1 where (a1.column1, a1.column2, a1.column3, a1.column4, a1.column5, a1.column6, a1.column7, a1.column8, a1.column9,
    a1.column10) in ( select a2.column1, a2.column2, a2.column3, a2.column4, a2.column5, a2.column6, a2.column7, a2.column8, a2.column9,
    a2.column10 from table1 a2 where column20 = '<condn>')
    The table1 used here is same in outer query as well as the sub query. this is a example of what we use here, and the table1 contains 30 million rows. Though, creating index with 10 columns can be a option, we already have a unique index with 11 columns(which includes 10 from this query) and will that be helpful in anyway? or the same existing index can be forced?
    Thanks a lot for ur time

    Depending on the selectivity of column20 I am not sure Index is the best way to go. It might perform better with two full scans and a hash-join.
    Anyway, I do prefer the syntax:
    select /*+ leading(a2) */ a1.*
    from table1 a1, table1 a2
    where a2.column20 = '<condn>'
    and a1.column1 = a2.column1
    and a1.column2 = a2.column2
    and  a1.column3 = a2.column3
    and  a1.column4 = a2.column4
    and  a1.column5 = a2.column5
    and  a1.column6 = a2.column6
    and  a1.column7 = a2.column7
    and  a1.column8 = a2.column8
    and  a1.column9 = a2.column9
    and  a1.column10 = a2.column10;I've added a leading hint to tell oracle that the start table is a2. Might be useless.
    Ensure your stats are up to date. You might need histograms here if your column20 is skewed.
    Hope this helps,
    François
    Edited by: Francois Berger on Oct 24, 2008 1:47 AM

  • Help on tuning this query

    Hi all i have this query:
    SELECT VTA.CO_VENDEDOR
    ,VTA.NB_VENDEDOR
    ,VTA.CO_CLASE_CLIENTE
    ,VTA.DE_CLASE_CLIENTE
    ,VTA.CO_CLIENTE
    ,VTA.NB_CLIENTE
    ,VTA.MN_VENDIDO
    ,NVL(DEV.MN_DEVUELTO,0) MN_DEVUELTO
    ,VTA.MN_VENDIDO - NVL(DEV.MN_DEVUELTO,0) MN_NETO_VENDIDO
    ,NVL(COB.MN_COBRADO,0) MN_COBRADO
    ,NVL(COB.MN_DESCUENTO,0) MN_DESCUENTO
    ,NVL(COB.MN_COBRADO,0) + NVL(COB.MN_DESCUENTO,0) MN_COBRADO_MAS_DESCUENTO
    FROM (SELECT FCL.CTV_CO_CLASE_ORGANIZACION CO_CLASE_CLIENTE
    ,FCL.CTV_DE_CLASE_ORGANIZACION DE_CLASE_CLIENTE
    ,FCL.CO_ORGANIZACION CO_CLIENTE
    ,FCL.ORG_NB_CLIENTE NB_CLIENTE
    ,FCL.PER_CO_IDENTIFICACION_VENDEDOR CO_VENDEDOR
    ,FCL.PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    FCL.PER_NB_PRIMER_APELLIDO_VEND NB_VENDEDOR
    ,SUM((CA_PRODUCTO * (PR_PRODUCTO - NVL(PR_DESCUENTO_PRODUCTO,0))) * (1 - NVL(PC_DESCUENTO,0))) MN_VENDIDO
    FROM S04_FACTURA_CLIENTE_TOTAL_R FCL
    ,S04_FACTURA_CLIENTE_PRD_TOT FPR
         ,(SELECT FCP.FAC_ID_FACTURA
    ,SUM(DDC.MN_DEBITO_CREDITO) /
    SUM(FCP.CA_PRODUCTO * (FCP.PR_PRODUCTO - NVL(FCP.PR_DESCUENTO_PRODUCTO,0))) PC_DESCUENTO
         FROM S04_DOCUMENTO_DEBITO_CREDITO_R DDC
    ,S04_FACTURA_CLIENTE_PRD_TOT FCP
         WHERE DDC.ID_DOCUMENTO_REFERENCIA = FCP.FAC_ID_FACTURA
    AND DDC.TDC_IN_DEBITO_CREDITO = 'C'
    GROUP BY FCP.FAC_ID_FACTURA) DCT
    WHERE FCL.ID_REGISTRO = FPR.FAC_ID_FACTURA
    AND DCT.FAC_ID_FACTURA (+) = FPR.FAC_ID_FACTURA
    AND FCL.FE_EMISION BETWEEN to_date('01112009','ddmmyyyy') AND to_date('29112009','ddmmyyyy')
    AND FCL.ORG_ID_CLIENTE = NVL(NULL,FCL.ORG_ID_CLIENTE)
    AND FCL.PER_ID_VENDEDOR = NVL(7647771,FCL.PER_ID_VENDEDOR)
                   AND FCL.CTV_CO_ESTADO_DOCUMENTO = 'EM'
    AND FCL.UBG_ID_UBICACION_CLIENTE in (SELECT ID_REGISTRO
    FROM S00_UBICACION_GEOGRAFICA
    START WITH ID_REGISTRO = nvl(NULL, FCL.UBG_ID_UBICACION_CLIENTE)
    CONNECT BY PRIOR ID_REGISTRO = UBG_ID_UBICACION_PADRE)
    GROUP BY FCL.CTV_CO_CLASE_ORGANIZACION
    ,FCL.CTV_DE_CLASE_ORGANIZACION
    ,FCL.CO_ORGANIZACION
    ,FCL.ORG_NB_CLIENTE
    ,PER_CO_IDENTIFICACION_VENDEDOR
    ,PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    PER_NB_PRIMER_APELLIDO_VEND) VTA
    ,(SELECT NDC .CTV_CO_CLASE_ORGANIZACION CO_CLASE_CLIENTE
    ,NDC.CTV_DE_CLASE_ORGANIZACION DE_CLASE_CLIENTE
    ,NDC.CO_CLIENTE
    ,NDC.ORG_NB_CLIENTE NB_CLIENTE
    ,FCL.PER_CO_IDENTIFICACION_VENDEDOR CO_VENDEDOR
    ,FCL.PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    FCL.PER_NB_PRIMER_APELLIDO_VEND NB_VENDEDOR
    ,SUM(CA_PRODUCTO * (PR_PRODUCTO - NVL(PR_DESCUENTO_PRODUCTO,0))) MN_DEVUELTO
    FROM S06_NOTA_DEBITO_CREDITO_TOT_R NDC
         ,S06_DETALLE_NOTA_DEB_CRED_TOT DND
    ,S04_FACTURA_CLIENTE_TOTAL_R FCL
         WHERE NDC.ID_REGISTRO = DND.NDC_ID_NOTA_DEBITO_CREDITO
    AND NDC.CTV_CO_TIPO_DOCUMENTO = SK00_BUSCAR.F_VCT('CO_TIPO_DOCUMENTO_NOTA_CREDITO_FISCAL')
    AND NDC.CTV_CO_ESTADO_DOCUMENTO = SK00_BUSCAR.F_VCT('CO_ESTADO_DOCUMENTO_EMITIDO')
         AND NDC.FE_EMISION BETWEEN to_date('01112009','ddmmyyyy') AND to_date('29112009','ddmmyyyy')
         AND (NDC.CTV_CO_MOTIVO_NOTA = SK00_BUSCAR.F_VCT('CO_MOTIVO_NOE_DEVOLUCION')
         OR NDC.CTV_CO_MOTIVO_NOTA = SK00_BUSCAR.F_VCT('CO_MOTIVO_AJUSTE_PRECIO_NCD'))
    AND NDC.ORG_ID_CLIENTE = NVL(NULL,NDC.ORG_ID_CLIENTE)
    AND FCL.ID_REGISTRO = NDC.FAC_ID_FACTURA
                             AND FCL.CTV_CO_ESTADO_DOCUMENTO = 'EM'
    AND FCL.UBG_ID_UBICACION_CLIENTE in (SELECT ID_REGISTRO
    FROM S00_UBICACION_GEOGRAFICA
    START WITH ID_REGISTRO = nvl(NULL, FCL.UBG_ID_UBICACION_CLIENTE)
    CONNECT BY PRIOR ID_REGISTRO = UBG_ID_UBICACION_PADRE)
    GROUP BY NDC.CTV_CO_CLASE_ORGANIZACION
    ,NDC.CTV_DE_CLASE_ORGANIZACION
    ,NDC.CO_CLIENTE
    ,NDC.ORG_NB_CLIENTE
    ,FCL.PER_CO_IDENTIFICACION_VENDEDOR
    ,FCL.PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    FCL.PER_NB_PRIMER_APELLIDO_VEND) DEV
    ,(SELECT RCD .CTV_CO_CLASE_ORGANIZACION CO_CLASE_CLIENTE
    ,RCD.CTV_DE_CLASE_ORGANIZACION DE_CLASE_CLIENTE
    ,RCD.CO_ORGANIZACION CO_CLIENTE
    ,RCD.ORG_NB_DISTRIBUIDOR NB_CLIENTE
    ,FCL.PER_CO_IDENTIFICACION_VENDEDOR CO_VENDEDOR
    ,FCL.PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    FCL.PER_NB_PRIMER_APELLIDO_VEND NB_VENDEDOR
    ,SUM(RCD.MN_DESCUENTO) MN_DESCUENTO
    ,SUM(DECODE(SIGN(RCO.MN_COBRADO_ANTES - IVA.MN_IVA)
    ,-1
    ,RCD.MN_ABONO - ABS(RCO.MN_COBRADO_ANTES - IVA.MN_IVA)
    ,RCD.MN_ABONO)) MN_COBRADO
    FROM S06_RECIBO_COBRO_DOC_TOTAL_R RCD
    ,S04_FACTURA_CLIENTE_TOTAL_R FCL
    ,(SELECT ID_DOCUMENTO_REFERENCIA FAC_ID_FACTURA
    ,NVL(SUM(MN_DEBITO_CREDITO),0) MN_IVA
    FROM S04_DOCUMENTO_DEBITO_CREDITO_R
    WHERE TDC_CO_TIPO_DEBITO_CREDITO = SK00_BUSCAR.F_VCT('CO_TIPO_DEBITO_IVA')
    GROUP BY ID_DOCUMENTO_REFERENCIA) IVA
    ,(SELECT ID_REFERENCIA FAC_ID_FACTURA
    ,NVL(SUM(MN_ABONO),0) MN_COBRADO_ANTES
    FROM S06_RECIBO_COBRO_DOC_TOTAL_R
    WHERE FE_EMISION < to_date('01112009','ddmmyyyy')
    GROUP BY ID_REFERENCIA) RCO
         WHERE RCD.CTV_CO_ESTADO_DOCUMENTO = SK00_BUSCAR.F_VCT('CO_ESTADO_DOCUMENTO_EMITIDO')
         AND RCD.FE_EMISION BETWEEN to_date('01112009','ddmmyyyy') AND to_date('29112009','ddmmyyyy')
    AND RCD.ORG_ID_ORGANIZACION = NVL(NULL,RCD.ORG_ID_ORGANIZACION)
    AND FCL.CTV_CO_ESTADO_DOCUMENTO = 'EM'
                             AND FCL.ID_REGISTRO = RCD.ID_REFERENCIA
    AND FCL.UBG_ID_UBICACION_CLIENTE in (SELECT ID_REGISTRO
    FROM S00_UBICACION_GEOGRAFICA
    START WITH ID_REGISTRO = nvl(NULL, FCL.UBG_ID_UBICACION_CLIENTE)
    CONNECT BY PRIOR ID_REGISTRO = UBG_ID_UBICACION_PADRE)
    AND IVA.FAC_ID_FACTURA (+) = FCL.ID_REGISTRO
    AND RCO.FAC_ID_FACTURA (+) = FCL.ID_REGISTRO
    GROUP BY RCD.CTV_CO_CLASE_ORGANIZACION
    ,RCD.CTV_DE_CLASE_ORGANIZACION
    ,RCD.CO_ORGANIZACION
    ,RCD.ORG_NB_DISTRIBUIDOR
    ,FCL.PER_CO_IDENTIFICACION_VENDEDOR
    ,FCL.PER_NB_PRIMER_NOMBRE_VENDEDOR||' '||
    FCL.PER_NB_PRIMER_APELLIDO_VEND) COB
    WHERE VTA.CO_CLIENTE = DEV.CO_CLIENTE (+)
    AND VTA.CO_VENDEDOR = DEV.CO_VENDEDOR (+)
    AND VTA.CO_CLIENTE = COB.CO_CLIENTE (+)
    AND VTA.CO_VENDEDOR = COB.CO_VENDEDOR (+)
    ORDER BY VTA.NB_VENDEDOR
    ,VTA.CO_CLIENTE
    Is there a way i can influence the join method on this query ...?
    It's an XE DB.
    Regards, Luis ...!

    THIS IS THE PLAN:
    SQL> /
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=79006 Card=1 Bytes
    =253)
    1 0 SORT (ORDER BY) (Cost=79006 Card=1 Bytes=253)
    2 1 HASH JOIN (OUTER) (Cost=79005 Card=1 Bytes=253)
    3 2 HASH JOIN (OUTER) (Cost=78538 Card=1 Bytes=211)
    4 3 VIEW (Cost=78478 Card=1 Bytes=182)
    5 4 HASH (GROUP BY) (Cost=78478 Card=1 Bytes=232)
    6 5 FILTER
    7 6 HASH JOIN (OUTER) (Cost=78469 Card=1 Bytes=232
    8 7 TABLE ACCESS (BY INDEX ROWID) OF 'T04_FACTUR
    A_PRODUCTO' (TABLE) (Cost=2 Card=1 Bytes=46)
    9 8 NESTED LOOPS (Cost=25 Card=1 Bytes=213)
    10 9 VIEW OF 'V04_FACTURA_CLIENTE_TOTAL_R' (V
    IEW) (Cost=23 Card=1 Bytes=167)
    11 10 NESTED LOOPS (Cost=23 Card=1 Bytes=284
    12 11 NESTED LOOPS (OUTER) (Cost=23 Card=1
    Bytes=278)
    13 12 HASH JOIN (SEMI) (Cost=23 Card=1 B
    ytes=272)
    14 13 NESTED LOOPS (Cost=17 Card=1 Byt
    es=259)
    15 14 NESTED LOOPS (Cost=16 Card=1 B
    ytes=218)
    16 15 NESTED LOOPS (OUTER) (Cost=1
    6 Card=1 Bytes=212)
    17 16 NESTED LOOPS (Cost=15 Card
    =1 Bytes=179)
    18 17 NESTED LOOPS (Cost=15 Ca
    rd=1 Bytes=173)
    19 18 NESTED LOOPS (OUTER) (
    Cost=14 Card=1 Bytes=162)
    20 19 NESTED LOOPS (OUTER)
    (Cost=14 Card=1 Bytes=156)
    21 20 NESTED LOOPS (OUTE
    R) (Cost=14 Card=1 Bytes=150)
    22 21 NESTED LOOPS (OU
    TER) (Cost=14 Card=1 Bytes=144)
    23 22 NESTED LOOPS (
    Cost=14 Card=1 Bytes=105)
    24 23 SORT (UNIQUE
    ) (Cost=2 Card=1 Bytes=13)
    25 24 TABLE ACCE
    SS (FULL) OF 'T00_GRUPO_DATO_USUARIO_ACTIVO' (TABLE (TEMP))
    (Cost=2 Card=1 Bytes=13)
    26 23 TABLE ACCESS
    (BY INDEX ROWID) OF 'T04_FACTURA' (TABLE) (Cost=11 Card=1 B
    ytes=92)
    27 26 INDEX (RAN
    GE SCAN) OF 'I04_FAC_FE_EMISION' (INDEX) (Cost=10 Card=2)
    28 22 TABLE ACCESS (
    BY INDEX ROWID) OF 'T03_EMPRESA_SUCURSAL' (TABLE) (Cost=0 Ca
    rd=1 Bytes=39)
    29 28 INDEX (RANGE
    SCAN) OF 'I03_EMS_EMP' (INDEX) (Cost=0 Card=1)
    30 21 INDEX (UNIQUE SC
    AN) OF 'PK_PRY' (INDEX (UNIQUE)) (Cost=0 Card=1 Bytes=6)
    31 20 INDEX (UNIQUE SCAN
    ) OF 'PK_CAJ' (INDEX (UNIQUE)) (Cost=0 Card=1 Bytes=6)
    32 19 INDEX (UNIQUE SCAN)
    OF 'PK_PEC' (INDEX (UNIQUE)) (Cost=0 Card=1 Bytes=6)
    33 18 TABLE ACCESS (BY INDEX
    ROWID) OF 'T00_CONTENIDO_TABLA_VIRTUAL' (TABLE) (Cost=1 Car
    d=1 Bytes=11)
    34 33 INDEX (UNIQUE SCAN)
    OF 'PK_CTV' (INDEX (UNIQUE)) (Cost=0 Card=1)
    SORRY EXPLAIN OUTPUT TRUNCATED DUE TO SPACE RESTRICION .....
    Any help would be greatly appreciated.
    I believe there must be a way to influence on the tha costly HASH JOIN operation
    SQL> SPOOL OFF

  • Help in fine tuning this query

    My query id this,
    select a.event_id,a.document_id
    from sample_transaction a
    where exists (select 1 from sample_transaction b where b.event_id <> a.event_id and b.document_id = a.document_id );
    The execution plan is like this,
    SELECT STATEMENT Cost = 5596
    FILTER
    TABLE ACCESS FULL SAMPLE_TRANSACTION
    TABLE ACCESS BY INDEX ROWID SAMPLE_TRANSACTION
    INDEX RANGE SCAN I_SAMPLE_TRANSACTION_FK_EVT
    There are 6 million records in the table.
    How can i fine tune this query so that i get the results
    Example is
    my table has
    EVENT_ID DOCUMENT_ID
    100 200
    100 200
    101 201
    102 201
    103 203
    104 203
    105 204
    105 204
    106 106
    My result should be
    EVENT_ID DOCUMENT_ID
    101 201
    102 201
    103 203
    104 203
    null

    Based on your select statement, you will bring back every row in sample_transaction where the document is referenced by more than one event. Looking at it another way, the only rows skipped are those where the document is only used by one event.
    The full table scan is not necessarilly a problem since Oracle has to check all rows. You may improve performance if you add an index on document_id. That will allow Oracle to do a better query on the EXISTS clause. Remember to rebuild your statistics after adding the index.

  • Help tuning this connect by query

    Hi,
    Does anybody know how I can tune this query to run faster?:
    SELECT COUNT(*)
    FROM (
    SELECT PAR_ROW_ID, PAR_DIVN_ID, X_CHEIF, NAME, ATTRIB_01, LEVEL FROM (
    SELECT A.ATTRIB_01,A.PAR_ROW_ID, B.PAR_DIVN_ID,C.X_CHEIF, D.NAME FROM S_ORG_EXT_XM A
    JOIN S_ORG_EXT B ON A.PAR_ROW_ID=B.ROW_ID
    JOIN CX_SAP_POSTN C ON A.ATTRIB_01=C.X_BELONGSORGUNIT
    JOIN S_POSTN D ON C.X_SAP_ID = D.X_SAP_POSTN_ID )
    START WITH ATTRIB_01 = :B1
    CONNECT BY PRIOR PAR_DIVN_ID=PAR_ROW_ID
    ORDER BY LEVEL )
    WHERE X_CHEIF IS NOT NULL
    Explain Plan:
    30 SELECT STATEMENT
    29 SORT [AGGREGATE]
    28 SIEBEL. VIEW
    27 SORT [ORDER BY]
    26 CONNECT BY [WITH FILTERING]
    12 FILTER
    11 COUNT
    10 NESTED LOOPS
    7 NESTED LOOPS
    4 NESTED LOOPS
    1 SIEBEL.CX_SAP_POSTN TABLE ACCESS [FULL]
    3 SIEBEL.S_ORG_EXT_XM TABLE ACCESS [BY INDEX ROWID]
    2 SIEBEL.S_ORG_EXT_XM_ATT01_X INDEX [RANGE SCAN]
    6 SIEBEL.S_ORG_EXT TABLE ACCESS [BY INDEX ROWID]
    5 SIEBEL.S_ORG_EXT_P1 INDEX [UNIQUE SCAN]
    9 SIEBEL.S_POSTN TABLE ACCESS [BY INDEX ROWID]
    8 SIEBEL.S_POSTN_SAPID_X INDEX [RANGE SCAN]
    25 HASH JOIN
    13 CONNECT BY PUMP
    24 COUNT
    23 NESTED LOOPS
    20 NESTED LOOPS
    17 NESTED LOOPS
    14 SIEBEL.CX_SAP_POSTN TABLE ACCESS [FULL]
    16 SIEBEL.S_ORG_EXT_XM TABLE ACCESS [BY INDEX ROWID]
    15 SIEBEL.S_ORG_EXT_XM_ATT01_X INDEX [RANGE SCAN]
    19 SIEBEL.S_ORG_EXT TABLE ACCESS [BY INDEX ROWID]
    18 SIEBEL.S_ORG_EXT_P1 INDEX [UNIQUE SCAN]
    22 SIEBEL.S_POSTN TABLE ACCESS [BY INDEX ROWID]
    21 SIEBEL.S_POSTN_SAPID_X INDEX [RANGE SCAN]

    Who says it is not already running as fast as possible?
    In order to solve a problem, you need to know the problem. Stating something like 'a query is slow and should be faster' is stating an opinion - it is not describing a problem that can be resolved.
    It is easy to comment on the obvious, such as there are two full table scans on SIEBEL.CX_SAP_POSTN. But without additional information, those FTSs could well be a better choice.
    My suggestion is to turn a complex problem, into a bunch of smaller and simpler problems. If you suspect that the query could be faster, pull it apart. Test each step/component of the query in isolation. E.g. start with a single table and progressively add joins to determine what the CBO does with each additional join, what you're asking Oracle to do ito processing, and not only checking that the CBO does not make obvious errors, but that what you're asking via SQL is the best approach to get to the answer.

  • Need help in this query using Case Statement

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

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

  • Help on this query that is using sysdate

    Hi
    I'm trying to get the objects created on the current date using the following query
    select owner,object_name,object_type,created from dba_objects where created = TRUNC(SYSDATE);
    but i don't see any rows for this query;
    but when i use this query .
    SELECT owner,object_name,object_type,created FROM DBA_OBJECTS ORDER BY CREATED DESC;
    i see some objects created today. Can you help me out if i missed any thing in the sysdate query.
    thanks

    Try this..
    select owner,object_name,object_type,created
    from dba_objects
    where trunc(created) = TRUNC(SYSDATE);                                                                                                                                                                                                                                                       

  • Can any one help with this query please

    I have a table something as below
    Things_t
    Things Characteristic Char Value
    Item 1 Colour Red
    Item 1 Packaging
    Item 2 Shape Square
    Item 2 Brand Spunk
    Now i want to reterive an item with none of its char values as Null. Using the query “ select distinct things from things_t where char value is Null ” will fetch the item 1 also together with item 2. i want to fetch a record from thing for which none of the char values are Null such as Item 2. Can you please help me with this query.

    Try this:
    WITH t AS
    (SELECT 1 item_id, 17436 chr_id, 14225034 chr_val_id FROM dual UNION
    SELECT 1 item_id, 39 chr_id, 14276173 chr_val_id FROM dual UNION
    SELECT 1 item_id, 17774 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 1 item_id, 265 chr_id, 20502978 chr_val_id FROM dual UNION
    SELECT 1 item_id, 16978 chr_id, 797233 chr_val_id FROM dual UNION
    SELECT 1 item_id, 13092 chr_id, 5666917 chr_val_id FROM dual UNION
    SELECT 1 item_id, 15228 chr_id, 1209758 chr_val_id FROM dual UNION
    SELECT 2 item_id, 112 chr_id,  12705342 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6945 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 70 chr_id, 12597376 chr_val_id FROM dual UNION
    SELECT 2 item_id, 16832 chr_id, NULL chr_val_id FROM dual UNION
    SELECT 2 item_id, 7886 chr_id, 9588619 chr_val_id FROM dual UNION
    SELECT 2 item_id, 6986 chr_id, 2659351 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9531 chr_id, 8910943 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9798 chr_id, 8717531 chr_val_id FROM dual UNION
    SELECT 3 item_id, 17446 chr_id, 12266441 chr_val_id FROM dual UNION
    SELECT 3 item_id, 4830 chr_id, 13683090 chr_val_id FROM dual UNION
    SELECT 3 item_id, 9518 chr_id, 834772 chr_val_id FROM dual UNION
    SELECT 3 item_id, 11031 chr_id, 20233753 chr_val_id FROM dual UNION
    SELECT 3 item_id, 12564 chr_id, 2282478 chr_val_id FROM dual)
    SELECT DISTINCT item_id
    FROM   t
    MINUS
    SELECT DISTINCT item_id
    FROM   t
    WHERE  chr_val_id IS NULLOr this:
    SELECT item_id
    FROM  (SELECT   item_id,
                    MIN(NVL(chr_val_id, -1)) min_chr_val_id
           FROM     t
           GROUP BY item_id)
    WHERE  min_chr_val_id != -1Edited by: lee200 on Oct 15, 2012 9:22 AM

  • Pls help with this query

    Hello
    If I run the following query I correctly get a sum of the invoices for a particular BP for Jan 09
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    However I need the value of credit notes to be reflected in the figure so I amended the query to be:
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum])ELSE 0 END) - SUM(CASE WHEN  T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] -  T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    When I run this query the output is incorrect ................ Is the fault to do with my JOINS?
    Thanks

    Hi Just to clarify
    This query, which just totals any invoices for a BP for January, correctly returns a value of £9660.44 :
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] -  T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode  WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    This next query, which just totals any credit notes for a BP in January, correctly returns a value of £567.73 :
    SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN  T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] -  T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0  INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
    I want a query which return a value of 9660.44 minus 567.73 (ie £9092.71)
    My original effort at this returned -£82,608.65 !!!!!!!!!
    and Gordon's amended version returned -£40,281.74 !!!!!
    Thanks for your patience
    Steve

Maybe you are looking for

  • White Screen Safari 2.0.4

    We have developed a framed application and are having trouble with the content showing up in the main frame. The screen appears but is completely white. If you view the source, the content is all there. If you click anywhere on the screen, the page p

  • Web Based Setup / Not loading properly

    I'm having a problem with my WRT54G V6 (wireless router).  It happened few times since i got this router.   (A friend of mine also have a wireless router from Linksys, i don't know the model). We both have the same problem: - After running for a whil

  • Uxterm doesn't show utf-8 right-arrow

    In uxterm, I use a C program to print my remaining battery to zsh's $RPROMPT. One of the characters used is a utf-8 right arrow, code point U+2192. I am using the Terminus font. At the console, I can see the arrows. Could my uxterm be messed up? What

  • Can not get on line-nor acccess email using road runner

    I have stated my problem as clearly as I know how-can not access Mozilla Firefox to the internet or my e mail

  • Menu selection: Insert / Media / Image Viewer does not exists

    Hi, I cannot see menu Insert / Media / Image Viewer What am I missing? My version is from "Adobe Creative Suite 6 Design & Web Premium" --BRH