Help needed in Outer Joins

Hi,
The follwing is my query
select imdsrequest.*,request_material.*,request.request_num from imdsrequest join request_material on
(imdsrequest.REQUEST_ID = request_material.REQUEST_ID)
join REQUEST on (request.request_id = request_material.associated_request_id)
it executes fine. But i want to re-write this query using outer join . on the above query i am using ON condition for matching the columns.
can any one help me to restructure this with outer join so that i can give all the conditions using WHERE clause .
Edited by: 936693 on Jul 3, 2012 7:12 AM
Edited by: 936693 on Jul 3, 2012 7:13 AM

Hi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
See the forum FAQ {message:id=9360002}
936693 wrote:
Hi Folks ,
Thanks for the reply. i don't want to use "ON" condition. So can you please show me some example based on my requirement .
i will have to use where condition instead of on and have to use outer joins where and all require, please guide me .Why don't you want to use "ON"?
Are you using Oracle 8 (or earlier)? Always say what version of Oracle you're using.
Is this part of a homework assignment, where the teacher specifically said not to use ON? Then post the exact question and instructions. It would help if you gave a little context (what was being taught in the most recent lesson) so we can give you a better answer.
The only ways to avoid "ON" are
(1) don't join any tables
(2) do a CROSS JOIN
(3) use the notation
If an outer join is appropriate for your problem, then I don't recommend any of the above.

Similar Messages

  • Help need for Outer Join Query Mysterious Query...

    I have two table parent tablea and child tableb
    Now
    tableb has field by name table_id,keyword_name,keyword_value
    tablea has field by name
    table_id,table_name
    I need a query which would retrieve union of this two set...(for table_name)
    1)all table_name having keyword_name='abc' and value=12
    2)all the table_name that does not have 'abc' as keyword in tableb.....
    Please let me know how it can be done....

    Ok then, try this.
    select
        a.table_name
    from
        tablea a, tableb b
    where
        a.table_id = b.table_id
        and b.keyword_name = 'abc'
        and b.value = 12
    union
    select
        c.table_name
    from
        tablea c
    where
        not exists (select 1 from tableb d
                    where d.table_id = c.table_id
                          and d.keyword_name = 'abc'
                   ) ;To get a faster answer and avoid wrong guesses, I always find it helpful to show sample data and sample output when asking a SQL question. For example:
    TableA -----------------
    table_id    table_name
      1           AAA
      2           BBB
      3           CCC
      4           DDD
      5           EEE
    TableB -----------------
    table_id    keyword_name   value
        1             abc       12
        1             def       23
        2             abc       13
        4             def       12
        5             abc       13
        5             def       12
    Desired output of query: -----------------
    AAA
    CCC
    DDD

  • Need help with an Outer Join

    I have the following query which has an outer join and it works perfectly and shows publication paragraph which don't have any maintenance records with a value of 0.
    However when I add to the where clause conditions such as lpm.fac_ident and lpm.start_date, the null values no longer show up in the query(see second SQL statement).
    I am new at SQL and am just trying to make this outer join work. Any help would be appreciated.
    -- THIS WORKS
    SELECT m.publication_paragraph, pm.description, pm.frequency, count(l.publication_paragraph) ACTIVITIES_PERFORMED
    FROM lpm_paragraph_mapping_table m, lpm l, pm_requirements_table pm
    WHERE m.paragraph_alias_mapping = l.publication_paragraph (+)
    GROUP BY m.publication_paragraph, pm.description, pm.frequency
    order by count(l.publication_paragraph);
    -- THIS DOES NOT WORK
    SELECT m.publication_paragraph, pm.description, pm.frequency, count(l.publication_paragraph) ACTIVITIES_PERFORMED
    FROM lpm_paragraph_mapping_table m, lpm l, pm_requirements_table pm
    WHERE m.paragraph_alias_mapping = l.publication_paragraph (+)
    AND l.fac_ident = 'EWR' AND TO_CHAR(l.start_date, 'YYYY') = '2010'
    GROUP BY m.publication_paragraph, pm.description, pm.frequency
    order by count(l.publication_paragraph);
    Edited by: George Heller on Jun 30, 2011 9:47 AM

    fabio_silva wrote:
    Hi,
    I just didn't get. The table (pm_requirements_table pm) haven't any join ???
    Regards,Forgive the ASCII art, but your query looks like:
    ----------  JOIN ----------
    | Alias M | ====> | Alias L |
    | Alias PM |
    ----------- The table pm_requirements_table is not joined at all. Unless it has only a single row, then your resultset will have 1 row for each row in lpm_paragraph_mapping_table (because of the outer join) times the number of rows in pm_requirements_table. Consider this simplified example.
    SQL> select * from t;
            ID DESCR
             1 T 1
             2 T 2
    SQL> select * from t1;
            ID DESCR
             1 T1 1
             2 T1 2
             3 T1 3
             4 T1 4
    SQL> select 1.id, t.descr, t1.descr
      2  from t, t1;
            ID DESCR      DESCR
             1 T 1        T1 1
             1 T 1        T1 2
             1 T 1        T1 3
             1 T 1        T1 4
             1 T 2        T1 1
             1 T 2        T1 2
             1 T 2        T1 3
             1 T 2        T1 4Here, my table t is the result from your join between lpm_paragraph_mapping_table and lpm. My table t1 is your pm_requirements_table. You need something more like:
    SQL> select 1.id, t.descr, t1.descr
      2  from t, t1
      3  where t.id = t1.id;
            ID DESCR      DESCR
             1 T 1        T1 1
             1 T 2        T1 2Assuming you fix that join, you can use a sub-query to apply the predicates to the lpm as Centinul showed, or, if you are on a current version of Oracle, use ANSI join syntax wich would look something like:
    SELECT m.publication_paragraph, pm.description, pm.frequency,
            count(l.publication_paragraph) ACTIVITIES_PERFORMED
    FROM lpm_paragraph_mapping_table m,
       LEFT JOIN lpm l,
          ON m.paragraph_alias_mapping = l.publication_paragraph and
             l.fac_ident = 'EWR' AND
             TO_CHAR(l.start_date, 'YYYY') = '2010'
       JOIN pm_requirements_table pm
          on something
    GROUP BY m.publication_paragraph, pm.description, pm.frequency
    ORDER BY count(l.publication_paragraph);Leaving aside the missing join, the reason why your query as posted does not work is that when there is not a match on m.paragraph_alias_mapping = l.publication_paragraph Oracle will "make up" rows form lpm with all null columns for each row in lpm_paragraph_mapping_table where there is no match. So, when you compare those made up rows from lpm, you are effectively saying where NULL = 'EWR' and TO_CHAR(NULL, 'yyyy') = '2010'. Null is never equal to anything. Any comparision against null is unknown, so the made up rows get filtered out by your predicate.
    John

  • [8i] Need help with full outer join combined with a cross-join....

    I can't figure out how to combine a full outer join with another type of join ... is this possible?
    Here's some create table and insert statements for some basic sample data:
    CREATE TABLE     my_tab1
    (     record_id     NUMBER     NOT NULL     
    ,     workstation     VARCHAR2(4)
    ,     my_value     NUMBER
         CONSTRAINT my_tab1_pk PRIMARY KEY (record_id)
    INSERT INTO     my_tab1
    VALUES(1,'ABCD',10);
    INSERT INTO     my_tab1
    VALUES(2,'ABCD',15);
    INSERT INTO     my_tab1
    VALUES(3,'ABCD',5);
    INSERT INTO     my_tab1
    VALUES(4,'A123',5);
    INSERT INTO     my_tab1
    VALUES(5,'A123',10);
    INSERT INTO     my_tab1
    VALUES(6,'A123',20);
    INSERT INTO     my_tab1
    VALUES(7,'????',5);
    CREATE TABLE     my_tab2
    (     workstation     VARCHAR2(4)
    ,     wkstn_name     VARCHAR2(20)
         CONSTRAINT my_tab2_pk PRIMARY KEY (workstation)
    INSERT INTO     my_tab2
    VALUES('ABCD','WKSTN 1');
    INSERT INTO     my_tab2
    VALUES('A123','WKSTN 2');
    INSERT INTO     my_tab2
    VALUES('B456','WKSTN 3');
    CREATE TABLE     my_tab3
    (     my_nbr1     NUMBER
    ,     my_nbr2     NUMBER
    INSERT INTO     my_tab3
    VALUES(1,2);
    INSERT INTO     my_tab3
    VALUES(2,3);
    INSERT INTO     my_tab3
    VALUES(3,4);And, the results I want to get:
    workstation     sum(my_value)     wkstn_name     my_nbr1     my_nbr2
    ABCD          30          WKSTN 1          1     2
    ABCD          30          WKSTN 1          2     3
    ABCD          30          WKSTN 1          3     4
    A123          35          WKSTN 2          1     2
    A123          35          WKSTN 2          2     3
    A123          35          WKSTN 2          3     4
    B456          0          WKSTN 3          1     2
    B456          0          WKSTN 3          2     3
    B456          0          WKSTN 3          3     4
    ????          5          NULL          1     2
    ????          5          NULL          2     3
    ????          5          NULL          3     4I've tried a number of different things, googled my problem, and no luck yet...
    SELECT     t1.workstation
    ,     SUM(t1.my_value)
    ,     t2.wkstn_name
    ,     t3.my_nbr1
    ,     t3.my_nbr2
    FROM     my_tab1 t1
    ,     my_tab2 t2
    ,     my_tab3 t3
    ...So, what I want is a full outer join of t1 and t2 on workstation, and a cross-join of that with t3. I'm wondering if I can't find any examples of this online because it's not possible....
    Note: I'm stuck dealing with Oracle 8i
    Thanks!!

    Hi,
    The query I posted yesterday is a little more complicated than it needs to be.
    Since my_tab2.workstation is unique, there's no reason to do a separate sub-query like mt1; we can join my_tab1 to my_tab2 and get the SUM all in one sub-query.
    SELECT       foj.workstation
    ,       foj.sum_my_value
    ,       foj.wkstn_name
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    FROM       (     -- Begin in-line view foj for full outer join
              SELECT        mt1.workstation
              ,        SUM (mt1.my_value)     AS sum_my_value
              ,        mt2.wkstn_name
              FROM        my_tab1   mt1
              ,        my_tab2   mt2
              WHERE        mt1.workstation     = mt2.workstation (+)
              GROUP BY   mt1.workstation
              ,        mt2.wkstn_name
                    UNION ALL
              SELECT      workstation
              ,      0      AS sum_my_value
              ,      wkstn_name
              FROM      my_tab2
              WHERE      workstation     NOT IN (     -- Begin NOT IN sub-query
                                               SELECT      workstation
                                       FROM      my_tab1
                                       WHERE      workstation     IS NOT NULL
                                     )     -- End NOT IN sub-query
           ) foj     -- End in-line view foj for full outer join
    ,       my_tab3  mt3
    ORDER BY  foj.wkstn_name
    ,       foj.workstation
    ,       mt3.my_nbr1
    ,       mt3.my_nbr2
    ;Thanks for posting the CREATE TABLE and INSERT statements, as well as the very clear desired results!
    user11033437 wrote:
    ... So, what I want is a full outer join of t1 and t2 on workstation, and a cross-join of that with t3. That it, exactly!
    The tricky part is how and when to get SUM (my_value). You might approach this by figuring out exactly what my_tab3 has to be cross-joined to; that is, exactly what should the result set of the full outer join between my_tab1 and my_tab2 look like. To do that, take your desired results, remove the columns that do not come from the full outer join, and remove the duplicate rows. You'll get:
    workstation     sum(my_value)     wkstn_name
    ABCD          30          WKSTN 1          
    A123          35          WKSTN 2          
    B456          0          WKSTN 3          
    ????          5          NULL          So the core of the problem is how to get these results from my_tab1 and my_tab2, which is done in sub-query foj above.
    I tried to use self-documenting names in my code. I hope you can understand it.
    I could spend hours explaining different parts of this query in more detail, but I'm sure I'd waste some of that time explaining things you already understand. If you want an explanation of somthing(s) specific, let me know.

  • Help with left outer join

    Hi,
    I Have this database structure:
    Artist(IDA,Name,Year_b)
    Work(IDW,Title,IDA,IDL)
    City(IDL,IName,Nation)
    I Want to do a query that retrieve for one Artist all Works availables on database and,if present,the City where each work was realized.
    This SQL code is correct?
    SELECT Name,Title,IName
    FROM Artist a,
    Work w LEFT OUTER JOIN City c ON w.IDL=c.IDL
    WHERE w.IDA=a.IDA and a.IDA='2';
    If it's not correct, what type of correction need it?
    Thanks for Help
    Andrea

    If you asked... The original query works, but it is not "right".
    You should use either the old Oracle style of joins, like in the VM's query, or, better, the ANSI join style, recommended by Oracle (not a mix of them, like in your query):
    SELECT a.name,
           w.title,
           c.iname
    FROM  artist a
    JOIN  work  w
       ON  w.ida = a.ida
    LEFT OUTER JOIN city c
       ON  w.idl = c.idl
    WHERE  a.ida = '2'

  • Help requried with outer joins

    Hi All,
    I neeed a query help can any one please help me getting this done,
    for the below query
    select trunc(assumed_time_sql) AS date1,sum(rp.rebate_due)as sum1 from mn_date_dim dd ,mn_rebate_payment rp,mn_prc_program prc,mn_structured_doc sd
    where trunc(assumed_time_sql) in (TO_DATE('01-JUL-10','dd-mon-yy'),add_months(TO_DATE('01-JUL-10','dd-mon-yy'),-3),add_months(TO_DATE('01-JUL-10','dd-mon-yy'),-6),add_months(TO_DATE('01-JUL-10','dd-mon-yy'),-9))----input parameter
    AND trunc(dd.assumed_time_sql)=TRUNC(rp.start_date)
    AND prc.prc_program_id=rp.tiered_rebate_id
    AND sd.struct_doc_id=prc.struct_doc_id
    AND sd.struct_doc_id_num='M0000763'----input parameter
    group by trunc(assumed_time_sql);
    I am getting output as
    date1      sum1
    01-JAN-10      10
    01-APR-10     15
    01-JUL-10     20
    But i want my output to be as
    date1      sum1
    01-JAN-10      10
    01-APR-10     15
    01-JUL-10     20
    01-OCT-09 NULL
    I have tried with outer joins but this didn't help me
    Can any one please give me help in getting this done
    Thanks

    Hi,
    As SB pointed out, you'd better provide tables and data in order to be helped more efficiently.
    Anyway, you may want to investigate the use of NVL and/or DECODE so that you can translate NULL into valid values so they are returned to your select statement.
    HTH,
    Thierry
    Handle:  p78   
    Status Level:  Newbie (10) 
    Registered:  Mar 9, 2009 
    Total Posts:  60 
    Total Questions:  35 (30 unresolved)  Be kind to share your helpful / correct threads with other with marking them as ANSWERED
    Edited by: Urgent-IT on Feb 13, 2011 11:00 AM

  • Help on "Left Outer Join" migration

    Can somebody help me convert the following join to Oracle, thanks.
    SELECT ...
    FROM ats
    LEFT OUTER JOIN vw_S sv ON ats.SID = sv.SID AND ats.R <> 'H'
    LEFT OUTER JOIN vw_P ag ON ats.AID = ag.AID
    The Workbench said "Manual conversion required"

    Hi Jimmy,
    Oracle 9i now supports ISO/ANSI outer join syntax
    So no changes are required.
    T-SQL
    SELECT ...
    FROM ats
    LEFT OUTER JOIN vw_S sv ON ats.SID = sv.SID AND ats.R <> 'H'
    LEFT OUTER JOIN vw_P ag ON ats.AID = ag.AID
    Oracle 9i (has ISO/ANSI Join Syntax)
    SELECT ...
    FROM ats
    LEFT OUTER JOIN vw_S sv ON ats.SID = sv.SID AND ats.R <> 'H'
    LEFT OUTER JOIN vw_P ag ON ats.AID = ag.AID
    Oracle 8i
    SELECT ...
    FROM ats , vw_S sv , vw_P ag
    WHERE ats.SID = sv.SID (+)
    AND ats.R <> 'H'
    AND ats.AID = ag.AID (+)
    I have not tested the above but I believe they are equivalent.
    I hope this helps
    Dermot.

  • Help of LEFT OUTER JOIN

    Hi Experts,
    Can you please help me with the below query on LEFT OUTER JOIN. I just want to know whether the below 2 queries are the same.
    1. select * from FROM hrm2_requests req LEFT OUTER JOIN hrm2_request_complaints hrm_comp LEFT OUTER JOIN hrm2_swap_complaint_detail comp_dtl LEFT OUTER JOIN hrm2_swap_complaint comp
    ON comp_dtl.complaint_code = comp.complaint_code
    ON hrm_comp.complaint_detail_code = comp_dtl.complaint_detail_code
    ON req.work_order = hrm_comp.work_order
    2. select * from hrm2_requests req LEFT OUTER JOIN hrm2_request_complaints hrm_comp
    ON req.work_order = hrm_comp.work_order
    LEFT OUTER JOIN hrm2_swap_complaint_detail comp_dtl
    ON hrm_comp.complaint_detail_code = comp_dtl.complaint_detail_code
    LEFT OUTER JOIN hrm2_swap_complaint comp
    i.e. SELECT * FROM TABLE1 LEFT OUT JOIN TABLE2 LEFT OUTER JOIN TABLE3 ON CONDITION <<>>
    is this equal to
    SELECT * FROM TABLE1 LEFT OUTER JOIN TABLE2 ON CONDITION
    LEFT OUTER JOIN TABLE3 ON CONDITION
    because the cost of the 1st query is very high and i change the sql like 2nd, the cost reduced a lot.
    Thanks,
    Yuvaraaj.

    Each JOIN clause must contain an ON condition (except for CROSS JOIN but let's not go there) so your example isn't syntactically valid and I'm not sure what you are comparing to what. I would probably write it as:
    select *
    from   from hrm2_requests req
           left join hrm2_request_complaints hrm_comp
                on   hrm_comp.work_order = req.work_order
           left join hrm2_swap_complaint_detail comp_dtl
                on   comp_dtl.complaint_detail_code = hrm_comp.complaint_detail_code
           left join hrm2_swap_complaint comp
                on   comp.complaint_code = comp_dtl.complaint_codeThe order in which you arrange the join clauses will not affect performance.

  • Help need for ansi join

    This is the sample piece of code in oracle which I am trying to convert it to ANSI join kindly help all the in_ and P_ and v_ are coming as a in parameter
    select *
    FROM    fxrates_v fx1
    WHERE fx1.fx_close(+) = in_fx_close AND
               fx1.fxrate_currency (+) =   decode (v_agree_type, 'L', upper(v_pledge_leh_unsec_curr) ,
                                                     'C', upper(v_pledge_cp_unsec_curr),
                                                     decode(sign(v_pre_mrgn_val), -1, upper(v_pledge_leh_unsec_curr),
                                                     upper(v_pledge_cp_unsec_curr)))/

    Michael I think your one is not equivalent to the OP's one.
    Let's see an example on EMP:
    SQL> select * from emp
      2  where comm(+) = 300;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
    SQL> select * from emp
      2  where comm(+) = null;
    no rows selected
    SQL> select * from emp right outer join dual on comm = 300;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO D
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30 X
    SQL> select * from emp right outer join dual on comm = null;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO D
                                                                                          XIf you add the right outer join on dual and the input parameter is null you extract an all nulls row.
    But the original query doesn't behave this way, the .(+) symbols have no effect on the result either if the input parameter is not null and if it is null...
    This is why I simply ignored them in my re-arrangement...
    So I think you can't convert the original query in ANSI sql preserving the behaviour...
    Am I missing something?
    Max
    http://oracleitalia.wordpress.com

  • Help needed sorting out wiring

    Hi everyone,
    After recieving help from this forum on increasing my speed I have been connected to the master socket since my initial speed boost.
    This comes with its own problems mainly that I now cannot use the telephones
    So I invested in a Solwise (Am i allowed to name brands....other brands are available)
    Trouble is when ive looked at the wiring in my current socket and tried to follow the instructions for the new filtered socket things uncertain.
    in my current bt master socket I have wires coming out of most if not all locations and need to wire it into the new one.
    any help would be fab as I've been 2 weeks without a house phone
    Solved!
    Go to Solution.

    The only wires that you will need to reconnect are those on terminal 2 and 5 ( Blue and white of blue) that will go back on the new front plate using 2 and 5 again ( make sure they go back to the same as you remove)
    Did you get a speed increase with the front plate off and connected direct into test socket?
    (If I have helped you in any way to say "Thank You" please click on the star next to the message. Thank You)
    If I have solved your Issue please click the "Mark as accepted solution" button.

  • W350 help needed - runs out of room synching my Outlook Notes

    I have about 50 short notes saved in Outlook Notes that I would like to have synched with my W350 phone.  Trouble is, that when I synch, I run out of room for these Notes at around #40.
    My question:  My phone has a 2GB ram chip in it.  If I go to a bigger chip, will I be able to download all the notes?  Trouble is, I am not sure if the phone uses this extra memory for Notes, or if it's in some kind of built-in memory in the phone.  I don't keep music or anything other than Contacts and Calendar items on the phone.
    Thanks for your help.

    Hi there, unfortunately notes are sync'ed to the internal memory of the phone. The internal memory is about 14MB so it is limited, I'm not aware of any software to allow you to sync directly with the memory card

  • Urgent Help needed - locked out of my 7610!

    Hi all. Think I might have locked myself out of voice calls on my 7610 accidentally. Forgot to lock the keypad when I put the phone i my bag and when I retrieved it it had "###########" on the keypad. Now it won't allow me to make any outbound calls (including to customer services) but sms, emaila and WAP all still work. I've checked the sim and it works perfectly well in another handset. What do I do to get my voice calls back??!! (I'm an Orange user, but I think this is a handset rather than a network problem).

    One way to contact Orange Customer Services is to phone 07973 100 150 from any phone and follow the prompts.
    You could also try resetting your phone, but you will lose ALL your data on your phone such as contacts and calendar appointments etc.
    I would advise you to check with Orange first.
    Regards,
    Edward

  • Help needed on Equi join?

    I have 3 views and in that common column datatype is varchar. I have to use equi join to get the data from 3 view.
    In 3 views total records are 23668,111,3033
    when i wrote the join i am getting the output in lakhs. I don't whether the output is right or not.
    Please guide me on this.
    Waiting for valuable replies.
    Thanks and Regards
    Sridhar.

    If you're returning more rows than you expect, it means that you have 1 to many or even many to many join.
    I would re-write your query this way:
    SELECT p.facid, rxs.rxno, rxs.rxbatch,patlname,patfname,InitReview,LabelPrintedOn,packed,PlacedInTote,street1,zip,BatchDescr
    FROM Patients p
    INNER JOIN rX.dbo.rxs rxs
    ON p.facid = rxs.FacID
    INNER JOIN rX.dbo.RxBatches Batch
    ON p.facid= Batch.FacID
    WHERE p.facid in('CRH','LSRX') and Batch.batchdescr LIKE 'STAT%'
    I also suggest to add alias to every column in your query. Even if the column belongs to a particular table, adding the alias in front of it will make maintenance of this query much easier. Say, I have no idea if BatchDescr field belongs to the second table
    RxBatches or rxS, so I made a guess.
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Oracle table merge - JOIN help needed

    Can someone help me with this results we want:
    F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R C_TCN_NUM
    2007-08-16 54 1 2008-02-08 4.06 131 40617700776019668
    2007-08-16 54 1 2008-02-08 4.06 135 40623900776014856
    2007-08-16 54 1 2007-11-02 1022.08 135 60630500002011374
    2007-08-16 54 1 2008-02-08 1022.08 135 30632600004003338
    Below listed are the two tables and data combinations, which need to be linked in this process.
    SQL> desc test11
    Name Null? Type
    F_FCN_DT NOT NULL CHAR(10)
    F_FCN_NUM NOT NULL NUMBER(5)
    F_FCN_MED_CD NOT NULL CHAR(1)
    F_APPLD_DT NOT NULL CHAR(10)
    F_APPLD_AMT NOT NULL NUMBER(13,2)
    F_RSN_CD NOT NULL CHAR(3)
    SQL> select * from test11;
    F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R
    2007-08-16 54 1 2008-02-08 4.06 131
    2007-08-16 54 1 2008-02-08 4.06 135
    2007-08-16 54 1 2007-11-02 1022.08 135
    2007-08-16 54 1 2008-02-08 1022.08 135
    SQL> desc test12
    Name Null? Type
    F_FCN_DT NOT NULL CHAR(10)
    F_FCN_NUM NOT NULL NUMBER(9)
    F_FCN_MED_CD NOT NULL CHAR(1)
    F_APPLD_AMT NOT NULL NUMBER(23,6)
    C_TCN_NUM NOT NULL CHAR(17)
    SQL> select * from test12;
    F_FCN_DT F_FCN_NUM F F_APPLD_AMT C_TCN_NUM
    2007-08-16 54 1 4.06 40617700776019668
    2007-08-16 54 1 4.06 40623900776014856
    2007-08-16 54 1 1022.08 60630500002011374
    2007-08-16 54 1 1022.08 30632600004003338

    Hi,
    If you don't care which row from test11 gets paired with which row from test12 (as long as they're in the same group), then use the analytic ROW_NUMBER function to assign arbitrary ids to each row within each group, and add that id to the join condition, like this:
    WITH    test11_plus  AS
         SELECT  f_fcn_dt, f_fcn_num, f, f_appld_dt, f_appld_amt, f_r               -- or whatever you need
         ,     ROW_NUMBER () OVER ( PARTITION BY  f_fcn_dt, f_fcn_num, f, f_appld_amt     -- common columns only
                                   ORDER BY      NULL
                           )                    AS r_num
         FROM    test11
    ,     test12_plus  AS
         SELECT  f_fcn_dt, f_fcn_num, f, f_appld_amt, c_tcn_num                    -- or whatever you need
         ,     ROW_NUMBER () OVER ( PARTITION BY  f_fcn_dt, f_fcn_num, f, f_appld_amt     -- common columns only
                                   ORDER BY      NULL
                           )                    AS r_num
         FROM    test11
    SELECT     p11.*               -- Or list everything except r_num
    ,     p12.c_tcn_num
    FROM     test11_plus     p11
    JOIN     test12_plus     p12     ON     p11.f_fcn_dt     = p12.f_fcn_dt
                        AND     p11.f_fcn_num     = p12.f_fcn_num
                        AND     p11.f          = p12.f
                        AND     p11.f_appld_amt     = p12.f_appld_amt
                        AND     p11.r_num     = p12.r_num
    ;If the number of rows in the two tables may be different, then you may need an outer join.

  • Help tuning NESTED LOOPS OUTER joins

    Hello,
    I have inherited this nasty query (below) that is taking an awful time to complete (more than 2 hrs a day)
    The worst bit is that I need to outer join my fact table so many times as I need bit’s and pieces from other tables/mviews.
    When I look at the explain plan I see that this situation means that the cbo is doing several NESTED LOOPS OUTER join operations. I understand that these nested loops mean going through every row in my primary table to see if there is a match in the secondary table (much smaller) which makes it extremely inefficient, is this right?
    The stats on the tables are all refreshed daily.
    Any ideas on how I can improve the performance here?
    Thanks in advance!
    The query:
    explain plan for
    SELECT x.user_id AS user_id,
    x.login_name AS login_name,
    c.date_of_birth AS date_of_birth,
    x.registration_site AS registration_site,
    x.organisation AS organisation,
    c.user_title AS user_title,
    c.first_name AS first_name,
    c.last_name AS last_name,
    x.email_address AS email_address,
    x.user_status AS user_status,
    x.user_privilege AS user_access_privilege,
    x.date_registration AS date_registration,
    x.affiliate_id AS affiliate_id,
    x.mobile_number AS mobile_number,
    x.optional_parameter AS vt_number,
    gud.display_name AS chat_name,
    REPLACE (s4.address_line_1, ',', '') AS address_line_1,
    REPLACE (s4.address_line_2, ',', '') AS address_line_2,
    REPLACE (s4.town, ',', '') AS town,
    REPLACE (s4.county, ',', '') AS county,
    REPLACE (s4.postcode, ',', '') AS postcode,
    s4.country AS country,
    s3.last_login AS last_login_date,
    x.email_send_newsletter AS email_send_newsletter,
    x.email_give_details_thirdparty AS email_give_details_thirdparty,
    NVL (ia.cash_balance, 0) AS current_cash_balance,
    NVL (ia.bonus_balance, 0) AS current_bonus_balance,
    x.external_affiliate_id AS external_affiliate_id,
    r.currency_code AS currency,
    NVL (ia.points_balance, 0) AS current_loyalty_points_balance,
    p.status AS buyer_status,
    NVL (ia.bi_bonus_balance, 0) AS current_bi_bonus_balance,
    NVL (ia.pending_balance, 0) AS current_pending_balance,
    l.level_name AS current_loyalty_level,
    l.date_level_achieved AS date_level_achieved,
    NVL (l.current_period_loyalty_points, 0) AS current_period_loyalty_points,
    r.region AS user_region,
    x.registration_platform AS registration_platform,
    x.external_user_name AS external_user_name,
    c.home_number AS home_number,
    pr.code AS reg_promo_code,
    g.date_first_buy AS date_first_buy
    FROM gl_user_registrations x,
    gl_region r,
    MVW_USER_BALANCES ia,
    gl_customers c,
    gl_user_display_names gud,
    gl_user_last_login s3,
    (SELECT z.user_id AS user_id,
    z.address_line_1 AS address_line_1,
    z.address_line_2 AS address_line_2,
    z.town AS town,
    z.county AS county,
    z.postcode AS postcode,
    z.country AS country
    FROM gl_user_addresses z
    WHERE z.is_current = 1) s4,
    gl_user_buyer_mapping upm,
    gl_buyer p,
    mvw_user_loyalty_points l,
    MVW_USER_PROMO_CODE_REG pr,
    MVW_USER_FIRST_BUY_DATE g
    WHERE x.base_region = r.region
    AND x.user_id = ia.user_id (+)
    AND x.customer_id = c.customer_id(+)
    AND x.user_id = gud.user_id (+)
    AND x.user_id = s4.user_id (+)
    AND x.user_id = s3.user_id (+)
    AND x.user_id = upm.user_id (+)
    AND upm.buyer_id = p.buyer_id
    AND x.user_id = l.user_id (+)
    AND x.user_id = pr.user_id (+)
    AND x.user_id = g.user_id (+);
    select * from table(dbms_xplan.display);
    Plan hash value: 2158171613
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 100 | 63100 | 135 (1)| 00:00:01 |
    | 1 | NESTED LOOPS OUTER | | 100 | 63100 | 135 (1)| 00:00:01 |
    | 2 | NESTED LOOPS OUTER | | 100 | 60600 | 120 (1)| 00:00:01 |
    | 3 | NESTED LOOPS OUTER | | 100 | 57100 | 105 (1)| 00:00:01 |
    | 4 | NESTED LOOPS OUTER | | 100 | 55400 | 90 (2)| 00:00:01 |
    | 5 | NESTED LOOPS OUTER | | 100 | 53600 | 70 (2)| 00:00:01 |
    |* 6 | HASH JOIN | | 100 | 47000 | 55 (2)| 00:00:01 |
    | 7 | TABLE ACCESS FULL | GL_REGION | 18 | 252 | 2 (0)| 00:00:01 |
    | 8 | NESTED LOOPS OUTER | | 100 | 22800 | 52 (0)| 00:00:01 |
    | 9 | NESTED LOOPS OUTER | | 100 | 19700 | 47 (0)| 00:00:01 |
    | 10 | NESTED LOOPS OUTER | | 100 | 17600 | 37 (0)| 00:00:01 |
    | 11 | NESTED LOOPS | | 100 | 15800 | 27 (0)| 00:00:01 |
    | 12 | NESTED LOOPS | | 102 | 2754 | 17 (0)| 00:00:01 |
    | 13 | TABLE ACCESS FULL | GL_BUYER | 6143K| 64M| 2 (0)| 00:00:01 |
    | 14 | TABLE ACCESS BY INDEX ROWID| GL_USER_BUYER_MAPPING | 1 | 16 | 1 (0)| 00:00:01 |
    |* 15 | INDEX RANGE SCAN | GL_USER_BUYER_MAPPPING_IX | 1 | | 1 (0)| 00:00:01 |
    | 16 | TABLE ACCESS BY INDEX ROWID | GL_USER_REGISTRATIONS | 1 | 131 | 1 (0)| 00:00:01 |
    |* 17 | INDEX UNIQUE SCAN | PK_GL_USER_REGISTRATIONS | 1 | | 1 (0)| 00:00:01 |
    | 18 | TABLE ACCESS BY INDEX ROWID | GL_USER_LAST_LOGIN | 1 | 18 | 1 (0)| 00:00:01 |
    |* 19 | INDEX UNIQUE SCAN | GL_USER_LAST_LOGIN_PK | 1 | | 1 (0)| 00:00:01 |
    | 20 | TABLE ACCESS BY INDEX ROWID | GL_USER_DISPLAY_NAMES | 1 | 21 | 1 (0)| 00:00:01 |
    |* 21 | INDEX UNIQUE SCAN | PK_GL_USER_DISPLAY_NAMES | 1 | | 1 (0)| 00:00:01 |
    | 22 | TABLE ACCESS BY INDEX ROWID | GL_CUSTOMERS | 1 | 31 | 1 (0)| 00:00:01 |
    |* 23 | INDEX UNIQUE SCAN | PK_GL_CUSTOMERS | 1 | | 1 (0)| 00:00:01 |
    |* 24 | TABLE ACCESS BY INDEX ROWID | GL_USER_ADDRESSES | 1 | 66 | 1 (0)| 00:00:01 |
    |* 25 | INDEX RANGE SCAN | IX_GL_USER_ADDRESSES1 | 1 | | 1 (0)| 00:00:01 |
    | 26 | MAT_VIEW ACCESS BY INDEX ROWID | MVW_USER_FIRST_BUY_DATE | 1 | 18 | 1 (0)| 00:00:01 |
    |* 27 | INDEX RANGE SCAN | MVW_USER_FS_DATE_IDX | 1 | | 1 (0)| 00:00:01 |
    | 28 | MAT_VIEW ACCESS BY INDEX ROWID | MVW_USER_PROMO_CODE_REG | 1 | 17 | 1 (0)| 00:00:01 |
    |* 29 | INDEX RANGE SCAN | MVW_USER_PROMO_CODE_IDX | 1 | | 1 (0)| 00:00:01 |
    | 30 | MAT_VIEW ACCESS BY INDEX ROWID | MVW_USER_LOYALTY_POINTS | 1 | 35 | 1 (0)| 00:00:01 |
    |* 31 | INDEX RANGE SCAN | MVW_USER_LYP_IDX | 1 | | 1 (0)| 00:00:01 |
    | 32 | MAT_VIEW ACCESS BY INDEX ROWID | MVW_USER_BALANCES | 1 | 25 | 1 (0)| 00:00:01 |
    |* 33 | INDEX RANGE SCAN | MVW_USER_BALANCES_IDX | 1 | | 1 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    6 - access("X"."BASE_REGION"="R"."REGION")
    15 - access("UPM"."BUYER_ID"="P"."BUYER_ID")
    17 - access("X"."USER_ID"="UPM"."USER_ID")
    19 - access("X"."USER_ID"="S3"."USER_ID"(+))
    21 - access("X"."USER_ID"="GUD"."USER_ID"(+))
    23 - access("X"."CUSTOMER_ID"="C"."CUSTOMER_ID"(+))
    24 - filter("Z"."IS_CURRENT"(+)=1)
    25 - access("X"."USER_ID"="Z"."USER_ID"(+))
    27 - access("X"."USER_ID"="G"."USER_ID"(+))
    29 - access("X"."USER_ID"="PR"."USER_ID"(+))
    31 - access("X"."USER_ID"="L"."USER_ID"(+))
    33 - access("X"."USER_ID"="IA"."USER_ID"(+))

    Hi,
    1) What you are saying about nested loops is true about any join (except, of course, cartesian joins): you are taking rows from rowsource A and find matching rows from rowsource B. This doesn't make a join method efficient or inefficient.
    2) The plan you posted does not indicate any performance problem whatsoever. I know you have one, but it's not possible to address it without having any information about it. Trace it, get dbms_xplan.display_cursor dump with rowsource stats, or real-time SQL monitoring report (if your version and license allow it) and post the results here, then we'd be able to help
    3) One efficient way to perform queries of your type (big fact table joined to a bunch of small dimension tables) is star transformation, but there are certain pre-requisites for that (like bitmap indexes on FK constraints) -- please read the documentation on star queries/transformations and see if that is an option for you
    Best regards,
    Nikolay

Maybe you are looking for

  • Why "TDM DSP Usage" greyed out?

    I'm using Logic Pro 7.1.0 on a G5 running OSX 10.4.2 with an HD3 system. I want to check my TDM DSP usage but it's "greyed out" in the Audio menu, non functional.. anybody know why? thanks Ed A

  • Getting data from some source and insert into corresponding source

    hi all, i am using db10g. i have like like MMA : add1:add2:add3'the above said is the format and actual data will be something like MMA : bank street : no:32 : tel: +9127546663 add1 = bank street add2 = no:32 add3 = tel: +9127546663 my requirement to

  • Irritating blue background.

    Hi to you all, I have a problem whenever I import SWF files into Dreamweaver, which should have a black background, then upload them online. As soon as I check how the pages look in a browser, there is a very noticable blue background which is basica

  • Termination error during Offcycle payroll run

    Hi All, Please help me with this error: when I do off cycle payroll run for one of employees I am getting the following error. Termination in operation ERROR                                              00000010                                       

  • When clicking into any of the Offset, Gamma or Gain "Circles", the color of my clip completely changes for that time!?

    Hi Guys, I really can't figure this one out. When color correcting my clips and I click on one of the HUD Controls such as the Overall Offset, the preview image of the clip changes to an extreme blue color, no matter where I drag the knob. As soon as