Update with an Outer Join

I want to update a column in a table by selecting a value from one of three tables (in a particular order) based on whether the key value in the table being updated is in one (or more) of those tables.
In other words:
There are four tables
The main table, main_table, has columns "key" and "title"; all rows have a key value but their title values are null
The three column lookup tables, table_1, table_2, and table_3; each also has columns "key" and "title", but in this case, each row has its key and title values not null
Each main_table's title is set as follows:
If the key value is in table_1, use the title from tabke_1
If the key value is not in table_1 but it is in table_2, use the title from table_2
If the key value is not in table_1 or table_2 but it is in table_3, use the title from table_3
If the key value is not in any of the three other tables, set it to null
I am also trying not to use the "(+)" terminology.
Is there any way of writing a single UPDATE statement that does this?
I can't find any way of doing it off the top of my head without using three separate UPDATE statements (one using table_1, one using table_2, and one using table_3, with the latter two limited to "main_table.title is null".
-- Don

This?
SQL> select * from main_table;
       KEY TITLE
         1
         2
         3
         4
         5
         6
6 rows selected
SQL>
SQL> update main_table mt set title =
2 (select title from
3 (
4 select v1.key, v1.title from
5 (
6 select key, title, 1 sortcol from table_1 union
7 select key, title, 2 sortcol from table_2 union
8 select key, title, 3 sortcol from table_3
9 ) v1
10 inner join
11 (
12 select key, min(sortcol) sortcol from
13 (
14 select key, 1 sortcol from table_1 union
15 select key, 2 sortcol from table_2 union
16 select key, 3 sortcol from table_3
17 )
18 group by key
19 ) v2
20 on v1.key=v2.key and v1.sortcol = v2.sortcol
21 )
22 where key = mt.key);
6 rows updated
SQL> select * from main_table;
       KEY TITLE
         1 t11
         2 t12
         3 t23
         4 t24
         5
         6 t36
6 rows selectedAnd the content of table_1, table_2, table_3 is:
SQL> select * from table_1;
       KEY TITLE
         1 t11
         2 t12
SQL> select * from table_2;
       KEY TITLE
         3 t23
         4 t24
         1 t21
SQL> select * from table_3;
       KEY TITLE
         6 t36
         4 t34

Similar Messages

  • Update Statement with left outer join

    hi,
    i have to update a column in table "a" from table "b" and both of them joined with left outer join
    How can I do this
    Thanks in Advance

    Please consider the following when you post a question. This would help us help you better
    1. New features keep coming in every oracle version so please provide Your Oracle DB Version to get the best possible answer.
    You can use the following query and do a copy past of the output.
    select * from v$version 2. This forum has a very good Search Feature. Please use that before posting your question. Because for most of the questions
    that are asked the answer is already there.
    3. We dont know your DB structure or How your Data is. So you need to let us know. The best way would be to give some sample data like this.
    I have the following table called sales
    with sales
    as
          select 1 sales_id, 1 prod_id, 1001 inv_num, 120 qty from dual
          union all
          select 2 sales_id, 1 prod_id, 1002 inv_num, 25 qty from dual
    select *
      from sales 4. Rather than telling what you want in words its more easier when you give your expected output.
    For example in the above sales table, I want to know the total quantity and number of invoice for each product.
    The output should look like this
    Prod_id   sum_qty   count_inv
    1         145       2 5. When ever you get an error message post the entire error message. With the Error Number, The message and the Line number.
    6. Next thing is a very important thing to remember. Please post only well formatted code. Unformatted code is very hard to read.
    Your code format gets lost when you post it in the Oracle Forum. So in order to preserve it you need to
    use the {noformat}{noformat} tags.
    The usage of the tag is like this.
    <place your code here>\
    7. If you are posting a *Performance Related Question*. Please read
       {thread:id=501834} and {thread:id=863295}.
       Following those guide will be very helpful.
    8. Please keep in mind that this is a public forum. Here No question is URGENT.
       So use of words like *URGENT* or *ASAP* (As Soon As Possible) are considered to be rude.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Query with multiple outer joins

    I had a doubt with whether the following kind of query is valid with multiple outer-joins. The format of the query is something like this:-
    select A.col1, B.col2
    from table1 A, table2 B, table3 C where
    A.col3=B.col4(+) and B.col5=C.col6(+)
    This would mean the follwoing with regard to outer-joins in the query.
    1) fetch records with col3 in table A matching or not matching col4 in table B
    2) fetch records with col5 in table B matching or not matching col6 in table C
    So, this query is valid?
    I hope, my question is clear.
    Please, help in solving the doubt.
    regards

    This is valid and it works fine

  • How to have a condition with the outer join

    Hi, I'd like to know if, inside a report, there's a way to create a condition which include in some way an outer join.
    So, I have 2 custom folders:
    Tickets with 2 fields: Ticket_id, Inventory_item_id, category_id and iptv_sumptom
    Hierarchy with 4 fields: Symptom, Inv_item_id, Category_id and Group
    These 2 folders are joined in this way
    Tickets.inventory_item_id = Hierarchy.inv_item_id (+) AND Tickets.category_id = Hierarchy.category_id (+) AND Tickets.iptv_sumptom = Hierarchy.Symptom (+)
    Now, from the report I have a parameter based on the field Group to restrict dataset of the tickets.
    ...but when I choose a group and I run the report, the conditions inside the plsql query generated is like this
    WHERE (Tickets.inventory_item_id = Hierarchy.inv_item_id (+) AND Tickets.category_id = Hierarchy.category_id (+) AND Tickets.iptv_sumptom = Hierarchy.Symptom (+)) AND Hierarchy.Group = 'A'
    where the last condition is WITHOUT outer join !
    I need to have also the outer join on the Hierarchy.Group: AND Hierarchy.Group (+) = 'A', but from the Administrator I don't want to add another join with (+) for the Group. From Plus I didn't find any way to write (+).....
    Anybody knows another method or workaround to have also a filter (the condition Hierarchy.Group = 'A') with an outer join ?
    Thanks in advance
    Alex

    Hi,
    A workaround can be either use new objects or modify the existing ones to avoid the outer join.
    In order to do that you can add a dummy record to each custom folder and then join by it.
    For example if you will add to the
    Tickets will be:
    select Ticket_id, Inventory_item_id, category_id, iptv_sumptom
    from......
    union all
    select -1,-1,-1,'Some value' from dual
    And hierarchy will be:
    select Symptom, Inv_item_id, Category_id ,Group
    from......
    union all
    select 'Some value',-1,-1,'Some value' from dual
    Now you can create a full join between them without the need to outer join.
    Hope it will help
    Tamir

  • Issues with using Outer join in Data Template

    Hi all,
    this is my data in two tables .
    Table Ronny1
    colA colB
    1 u
    2 v
    3 x
    Table Ronny2
    colC colD
    1 q
    2 r
    This is my data template
    <dataTemplate name="MGF" dataSourceRef="Source1">
         <dataQuery>
              <sqlStatement name="Q1" dataSourceRef="Source1">
                   <![CDATA[select C, D  from Ronny2]]>
              </sqlStatement>
              <sqlStatement name="Q2" dataSourceRef="Source2">
                   <![CDATA[select A , B from lokesh1 where A = :C]]>
              </sqlStatement>
         </dataQuery>
         <dataStructure>
              <group name="G1" source="Q1">
                   <group name="G2" source="Q2">
                        <element name="A" value="A"/>
                        <element name="B" value="B"/>
                   </group>
              </group>
         </dataStructure>
    </dataTemplate>
    Now this would give me result as
    A B
    1 u
    2 v
    however my requirement is this
    A B
    1 u
    2 v
    3 x
    that is, i want to display all the rows of table Ronny1 and matching rows from table Ronny2. I tried modifiying my second sql as
    <![CDATA[select A , B from lokesh1 where A = :C (+)]]> ( using a outer join)
    but this does give me correct data and comes back with the cartesion product.
    Can anyone please answer this for me that how can we use the outer join in data templaes.
    Thanks
    Ronny

    hey vetsrini,
    sorry for the confusion here,
    actually i want to display all the rows from table Ronny1 and i am also pulling in the matching rows from table Ronny2 and my data structure looks as ( pulling in column D from table Ronny2)
    <dataStructure>
    <group name="G1" source="Q1">
    <group name="G2" source="Q2">
    <element name="A" value="A"/>
    <element name="B" value="B"/>
    <element name="D" value="D"/>
    </group>
    </group>
    </dataStructure>
    and this is what i want the output to be
    A B D
    1 u q
    2 v r
    3 x
    Hence the row where A <> C the data in the D column will be null, same as in the case of outer join.
    any suggestions
    Thx
    Ronny

  • Query with FULL OUTER JOIN , help pleaseeeeeeeeeeee...

    Hi everyone,
    I'm trying to write a query for a report in Oracle SQL, but i just can't figure out how to do it.
    I'm using Oracle 10g release 1.0 database and i execute my queris in SQL* PLUS ( eventually i'm gonna use them in Oracle Report Builder ) .
    here's what i have:
    i have four tables that are used for our inventory application. lets call them INCOMMING , INCOMMING_ITEMS , OUTGOING , OUTGOING_ITEMS.
    as you may have guessed , INCOMMING_ITEMS is the detail table for INCOMMING ( joined by IID column) and also OUTGOING_ITEMS is the detail table for OUTGOING ( joined by OID column ).
    here is the structure of them :
    INCOMMING
    IID varchar2
    CDATE date
    INCOMMING_ITEM
    IID varchar2
    PART_NO number
    QTY number
    OUTGOING
    OID varchar2
    CDATE date
    OUTGOING_ITEM
    OID varchar2
    PART_NO number
    QTY number
    now , the query i want, should return part_no , cdate , sum of OUTGOING qty , sum of INCOMMING qty .
    the result of the query should be sth like this :
    part_no     cdate     O_qty     I_qty
    100     01/05/06     10     0
    100     01/05/07     20     60
    200     01/06/02     0     50
    300     01/06/02     30     40
    this means that for some dates and for some parts, i may not have INCOMMING or OUTGOING data, but if at least one of the 2 last columns has a non-zero data, i should show the row ( like the first and third rows of my example result), and if both have data for the same PART_NO and the same CDATE, both should be showed in the same row. ( like the second row in my example result)
    i tried so much and came up with several huge and also time consuming queries, but then i read abt FULL OUTER JOIN somewhere and tried using that. here is what i came up with :
    SELECT
    PART_NO , CDATE , sum(II.QTY) I_QTY , SUM (OI.QTY) O_QTY
    FROM
         (OUTGOING O INNER JOIN OUTGOING_ITEM OI USING ( OID ) )
    FULL OUTER JOIN
         (INCOMMING I INNER JOIN INCOMMING_ITEM II USING ( IID ) )
    ON ( I.CDATE = O.CDATE AND II.PART_NO = OI.PART_NO)
    WHERE
    I.CDATE = :PARAMETER1
    AND O.CDATE = :PARAMETER1
    GROUP BY
    PART_NO , CDATE
    this query is short and fast , but the results r not what i expected. i mean, although i have used FULL OUTER JOIN in the query , but the results i get r sth like this :
    part_no     cdate     O_qty     I_qty
    100     01/05/07     20     60
    300     01/06/02     30     40
    which means only the rows that has both values are returned.
    any change i make to this query would make the SQL* PLUS hang , like when i use the cartesian product of two large tables, so i guess my changes wheren't in the right direction.
    i think its possible to write this query using FULL OUTER JOIN syntax, but i just can't find it.
    Can anybody pleaseeeeeeeeeeeee help me?
    thanx in advance,
    Maryam.

    Note: I wrote this on the fly -- hope there is no syntax errors, otherwise forgive me -- but you get the idea..
    select
    fromUnionAll.cdate, fromUnionAll.part_no,
    sum(fromUnionAll.O_qty) O_qty,
    sum(fromUnionAll.I_qty) I_qty
    from
    select
    iinner.cdate, iinner.part_no, 0 O_qty, iinner.I_qty
    from
    select
    i.cdate, ii.part_no,
    /* added the case only for the extreme case when there is
    no record anywhere for the given CDATE in INCOMMING_item */
    sum( ( case when ii.qty is not null then ii.qty else 0 end) ) I_qty
    from
    incomming i,
    incomming_item ii
    where
    i.iid = ii.iid (+)
    group by i.cdate, ii.part_no
    ) iinner
    union all
    select
    oinner.cdate, oinner.part_no, oinner.O_qty, 0 I_qty
    from
    select
    o.cdate, oi.part_no,
    /* added the case only for the extreme case when there is
    no record anywhere for the given CDATE in OUTGOING_item */
    sum( ( case when oi.qty is not null then oi.qty else 0 end) ) O_qty
    from
    outgoing o,
    outgoing_item oi
    where
    o.oid = oi.oid (+)
    group by o.cdate, oi.part_no
    ) oinner
    ) fromUnionAll
    group by fromUnionAll.cdate, fromUnionAll.part_no;
    --Samson                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • What is IS NULL doing in replacing NOT EXISTS operator with an outer join?

    DB version:10gR2
    An example from searchoracle.target.com
    The query which contains a NOT EXISTS operator
    SELECT M.ModulId, M.Modul FROM MODULE M
    WHERE NOT EXISTS
    ( SELECT PROJEKTMODUL.IdModul
      FROM PROJEKTMODUL
      WHERE M.ModulId = PROJEKTMODUL.IdModul
      AND PROJEKTMODUL.IdProjekt = 23 )is replaced by an equivalent outer join query
    select distinct
           M.ModulId
         , M.Modul
      from MODULE M
    left outer
      join PROJEKTMODUL
        on M.ModulId = PROJEKTMODUL.IdModul
       and PROJEKTMODUL.IdProjekt = 23
    where PROJEKTMODUL.IdProjekt is nullI don't understand what
    PROJEKTMODUL.IdProjekt is nullis doing in the above rewritten query.

    It is to perform an Anti-Join. As far as I know Oracle (starting with release 10g - I thought Release 1) rewrites your NOT EXISTS and NOT IN query to a Anti Join which looks like your second query.
    Look at the results of the query without the IS NULL predicate and see which records (from that set) should be selected to show the same results as the first query. The records of interest match the predicate IS NULL.

  • 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

  • Help Required with an OUTER JOIN Query

    Hi,
    I need a list of records carrying UserID and ALL SectionIDs and SectionTitles he/she is associated
    with so I created following query:
    SELECT UsersSections.UserID, Sections.SectionID, Sections.SectionTitle
    FROM UsersSections, Sections
    WHERE UsersSections.SectionID(+) = Sections.SectionID AND UsersSections.UserID = 1
    Where my tables are:
    - UsersSections (UserID, SectionID)
    - Sections (SectionID, SectionTitle)
    The problem is that if I exclude WHERE UserID = '1' clause the query return the desired result i.e. ALL Sections
    And ALL Users associated with the any Section(s). But I need results for a particular UserID but
    Including WHERE UserID = '1' clause only return records for that User only not the sections he/she belongs to which was the purpose of using OUTER JOIN.
    Any help will be highly appreciated.
    Thanks.
    AKG

    userssections.userid (+) = 1

  • Granting SELECT to user on VIEW with FULL OUTER JOIN fails?

    I have a quandary.
    Using Oracle 9i, I have created a simple view. When I perform a count on it, rows are returned.
    However, when I grant SELECT access to another user, they can't see the VIEW. The VIEW has a FULL OUTER JOIN operation in it.
    When I do the same thing using a regular join, it works.
    Any ideas why, please?
    SQL> conn ifsinfo/******@DB
    Connected.
    SQL> ed
    Wrote file afiedt.buf
      1  create view mctest3 as
      2  select
      3   vc.idcus ,
      4   ci.customer_id
      5  from
      6   ifsapp.vmo_company vc
      7  full outer join
      8   ifsapp.customer_info ci
      9  on
    10*  vc.custno = ci.customer_id
    SQL> /
    View created.
    SQL> select count(*) from mctest3;
      COUNT(*)
         73994
    SQL> GRANT SELECT ON MCTEST3 TO IFSAPP WITH GRANT OPTION;
    Grant succeeded.
    SQL> CONN IFSAPP/******@DB
    Connected.
    SQL> select count(*) from IFSINFO.MCTEST3;
    select count(*) from IFSINFO.MCTEST3
    ERROR at line 1:
    ORA-00942: table or view does not existbut with regular join:
    SQL> conn ifsinfo/******@DB
    Connected.
    SQL> create view mctest4 as
      2    select
      3     vc.idcus ,
      4     ci.customer_id
      5    from
      6     ifsapp.vmo_company vc, ifsapp.customer_info ci
      7    where vc.custno = ci.customer_id;
    View created.
    SQL> select count(*) from mctest4;
      COUNT(*)
         44269
    SQL> GRANT SELECT ON MCTEST4 TO IFSAPP WITH GRANT OPTION;
    Grant succeeded.
    SQL> conn ifsapp/******@DB
    Connected.
    SQL> select count(*) from IFSINFO.MCTEST4;
      COUNT(*)
         44269

    Hi,
    >>SQL> conn ifsinfo/******@DB
    Connected.
    SQL> ed
    Wrote file afiedt.buf
    1 create view mctest3 as
    2 select
    3 vc.idcus ,
    4 ci.customer_id
    5 from
    6 ifsapp.vmo_company vc
    7 full outer join
    8 ifsapp.customer_info ci
    9 on
    10* vc.custno = ci.customer_id
    SQL> /
    According to Note:244315.1, it is not possible to make a FULL OUTER JOIN on views owned by another user at the 9i version of Oracle. As above, do not use FULL OUTER JOIN on views owned by another user. Try to use outer join operator (+), and/or UNIONS instead.
    Cheers

  • Union among multiple select queries with full outer join

    Hello everyone,
    I have 3 different select queries (used FULL Outer Join) which work fine. Now I want to add Union to the results among them and pick the selected columns from each query in the final result. while doing so, I am getting an error as "right parenthesis missing". I am quite sure, it is not the real cause. I guess might be issue with the query structure.
    select j.pod, j.hostname, portal.hostname,saasc.hostname,a3s.hostname from -- * from
    Select J.Pod,J.Hostname, P.Pod Portal_Pod,P.Hostname Portal_Hostname
    From Total_Pod J
    full outer join Portal_Tmp P On (J.Pod = P.Pod And J.Hostname = P.Hostname) as portal
    Union
    Select J.Pod,J.Hostname, s.Pod saasc_Pod,s.Hostname saasc_Hostname
    From Total_Pod J
    full outer join Saasc_Tmp S  On (J.Pod = s.Pod And J.Hostname = s.Hostname) as saasc
    Union
    Select J.Pod,J.Hostname, a.Pod a3s_Pod,a.Hostname a3s_Hostname
    From Total_Pod J
    Full Outer Join A3s_Tmp A  On (J.Pod = A.Pod And J.Hostname = A.Hostname) as a3s
    )p.s: select * from (INNER QUERY); also does not work.
    Any help appreciated.
    Thanks in advance.

    With T as
    (Select J.Pod,J.Hostname, P.Pod Portal_Pod,P.Hostname Portal_Hostname
    From Total_Pod J
    full outer join Portal_Tmp P On (J.Pod = P.Pod And J.Hostname = P.Hostname) ),
    U as
    (Select J.Pod,J.Hostname, s.Pod saasc_Pod,s.Hostname saasc_Hostname
    From Total_Pod J
    full outer join Saasc_Tmp S  On (J.Pod = s.Pod And J.Hostname = s.Hostname) ),
    V as
    (Select J.Pod,J.Hostname, a.Pod a3s_Pod,a.Hostname a3s_Hostname
    From Total_Pod J
    Full Outer Join A3s_Tmp A  On (J.Pod = A.Pod And J.Hostname = A.Hostname) )
    Select T.Pod,T.Hostname,nvl(T.Portal_Hostname,'Not Available') portal,nvl(U.Saasc_Hostname,'Not Available') saasc,NVL(V.A3s_Hostname,'Not Available') a3s From T,U,V
    Where T.Pod = U.Pod
    And U.Pod = V.Pod
    And T.Hostname = U.Hostname
    And U.Hostname = V.Hostname

  • Repalcing UNIOn with full outer Join

    I have a query wihch looks like ,
    select colA,colB ,colC, coldD from A,B,C,D
    where (some join conditions)
    union
    select colA,colB ,colC, NULL from A,B,C
    where (some join conditions)
    This query is posing us serious performance issues and we want to tune the query.
    SO to remove the UNION operator we want to use FULL JOIN.
    Can you please let us know how to frame the full outer join in the above query,so that the result set intact.
    Thanks Much

      Here goes my first query that uses UNION :
    SELECT  from_it.seq_routing,
           from_it.milestone_routing_dt,
           from_it.milestone_type_desc,
           to_it.seq_routing,
           to_it.milestone_routing_dt,
           to_it.milestone_type_desc,
           to_it.delay_flag,
           to_it.acceptable_delay_flag,
           TAB_A.calendar_minutes, TAB_A.business_minutes,
           TAB_A.due_date, TAB_A.late_minutes,
           TAB_A.delay_concat_comments,TAB_C.TAB_C_key,
           TAB_C.pcd,TAB_C.ssn,
           mmp.MEASURE_NAME,
           api.METRICS_PARAMETER_KEY,
    api.METRICS_PARAMETER_PRODUCT_KEY,
    crt.JUSTIFICATION_CD,
    crt.JUSTIFICATION_DESC,
    crt.ROUTING_USER_SITE_DESC
      FROM TAB_A, TAB_B from_it, TAB_B to_it,
      TAB_C, TAB_E,TAB_D mmp,TAB_F api, TAB_G crt
    WHERE from_it.TAB_B_key =
                                      TAB_A.from_TAB_B_key
       AND to_it.TAB_B_key =
                                        TAB_A.to_TAB_B_key
       ANDTAB_C.TAB_C_key = from_it.TAB_C_key
       andTAB_C.TAB_C_key = to_it.TAB_C_key
       and TAB_E.pck =crt.pck
       and to_it.SEQ_ROUTING  =crt.SEQ_ROUTING
       and TAB_E.pcd =TAB_C.pcd
       and TAB_E.ssn =TAB_C.ssn
       andTAB_C.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')
       and TAB_A.METRICS_MEASURE_KEY =mmp.METRICS_MEASURE_KEY
       andapi.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')
       andTAB_C.TAB_C_key =api.TAB_C_KEY
       andapi.PARAMETER_TYPE = 'IN'
    and TAB_A.end_dt = TO_DATE('12/31/9999', 'MM/DD/YYYY')
    and CRT.end_dt  = TO_DATE('12/31/9999', 'MM/DD/YYYY')
    UNION
    SELECT  from_it.seq_routing,
           from_it.milestone_routing_dt,
           from_it.milestone_type_desc,
           to_it.seq_routing,
           to_it.milestone_routing_dt,
           to_it.milestone_type_desc,
           to_it.delay_flag,
           to_it.acceptable_delay_flag,
           TAB_A.calendar_minutes, TAB_A.business_minutes,
           TAB_A.due_date, TAB_A.late_minutes,
           TAB_A.delay_concat_comments,TAB_C.TAB_C_key,
           TAB_C.pcd,TAB_C.ssn,
           mmp.MEASURE_NAME,
           api.METRICS_PARAMETER_KEY,
    api.METRICS_PARAMETER_PRODUCT_KEY,
    NULL, NULL, NULL
      FROM TAB_A, TAB_B from_it, TAB_B to_it,
      TAB_C, TAB_E,TAB_D mmp,TAB_F api
    WHERE from_it.TAB_B_key =
                                      TAB_A.from_TAB_B_key
       AND to_it.TAB_B_key =
                                        TAB_A.to_TAB_B_key
       ANDTAB_C.TAB_C_key = from_it.TAB_C_key
       andTAB_C.TAB_C_key = to_it.TAB_C_key
       and TAB_E.pcd =TAB_C.pcd
       and TAB_E.ssn =TAB_C.ssn
       andTAB_C.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')
       and TAB_A.METRICS_MEASURE_KEY =mmp.METRICS_MEASURE_KEY
       andapi.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')
       andTAB_C.TAB_C_key =api.TAB_C_KEY
       andapi.PARAMETER_TYPE = 'IN'
       and to_it.SEQ_ROUTING is null
    and TAB_A.end_dt = TO_DATE('12/31/9999', 'MM/DD/YYYY')
    And the one with OUTER JOIN used :
    SELECT  from_it.seq_routing,
           from_it.milestone_routing_dt,
           from_it.milestone_type_desc,
           to_it.seq_routing,
           to_it.milestone_routing_dt,
           to_it.milestone_type_desc,
           to_it.delay_flag,
           to_it.acceptable_delay_flag,
           TAB_A.calendar_minutes, TAB_A.business_minutes,
           TAB_A.due_date, TAB_A.late_minutes,
           TAB_A.delay_concat_comments,TAB_C.TAB_C_key,
           TAB_C.pcd,TAB_C.ssn,
           mmp.MEASURE_NAME,
           api.METRICS_PARAMETER_KEY,
    api.METRICS_PARAMETER_PRODUCT_KEY,
    crt.JUSTIFICATION_CD,
    crt.JUSTIFICATION_DESC,
    crt.ROUTING_USER_SITE_DESC
      FROM TAB_A  JOIN
      TAB_B from_it
      on  from_it.TAB_B_key =
                                      TAB_A.from_TAB_B_key
                      and TAB_A.end_dt = TO_DATE('12/31/9999', 'MM/DD/YYYY')
             JOIN
      TAB_B to_it
      ON to_it.TAB_B_key =
                                        TAB_A.to_TAB_B_key
                        JOINTAB_C
    ONTAB_C.TAB_C_key = from_it.TAB_C_key
    ANDTAB_C.TAB_C_key = to_it.TAB_C_key    
    andTAB_C.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')                
      JOINTAB_D mmp
      ON TAB_A.METRICS_MEASURE_KEY =mmp.METRICS_MEASURE_KEY
      JOIN TAB_E
      ON  TAB_E.pcd =TAB_C.pcd
       and TAB_E.ssn =TAB_C.ssn
      -- and TAB_E.ssn = 'PFEARG'
    JOINTAB_F api
    ON   TAB_C.TAB_C_key =api.TAB_C_KEY
       andapi.PARAMETER_TYPE = 'IN'
       andapi.END_DT = TO_DATE('12/31/9999', 'MM/DD/YYYY')
      LEFT

  • Strange bug with Full Outer Join

    Hi guys,
    can you please confirm this behaviour on 11gR2? It used to work fine on 10gR1.
    Small test case:
    create table TEST_OBJECT_METADATA
      project_name  VARCHAR2(30 CHAR) not null,
      object_id     NUMBER not null
    create table TEST_OBJECT_INFO
      object_id          NUMBER not null,
      object_type        VARCHAR2(30) not null
    create table TEST_SUPPORTED_OBJECTS
      object_type    VARCHAR2(30 CHAR) not null,
      enabled        NUMBER(1));
    INSERT INTO TEST_OBJECT_METADATA VALUES ('NEW',1);
    INSERT INTO TEST_OBJECT_INFO VALUES (1, 'TABLE');
    INSERT INTO TEST_SUPPORTED_OBJECTS VALUES ('TABLE',1);
    COMMIT;When I execute the following query:
    WITH old_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'OLD'),
    new_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'NEW')
    SELECT src.object_type,
               t.enabled
      FROM (SELECT nvl(o.object_type, n.object_type) object_type
              FROM old_project o
              FULL OUTER JOIN new_project n
                ON o.object_id = n.object_id) src,
           test_supported_objects t
    WHERE src.object_type = t.object_type(+);I get the following output:
    OBJECT_TYPE     ENABLED
    TABLE             So, the outer join to test_supported_objects does not match.
    But if I force the FULL OUTER JOIN to run first using rownum:
    WITH old_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'OLD'),
    new_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'NEW')
    SELECT src.object_type,
               t.enabled
      FROM (SELECT nvl(o.object_type, n.object_type) object_type,
                           rownum
              FROM old_project o
              FULL OUTER JOIN new_project n
                ON o.object_id = n.object_id) src,
           test_supported_objects t
    WHERE src.object_type = t.object_type(+);I get the expected output:
    OBJECT_TYPE     ENABLED
    TABLE             1 Thanks in advance.

    Without mixing works fine:
    WITH old_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'OLD'),
    new_project AS
    (SELECT oi.object_type,
             oi.object_id
        FROM test_object_metadata om,
             test_object_info     oi
       WHERE om.object_id = oi.object_id
         AND project_name = 'NEW')
    SELECT src.object_type,
           t.enabled
      FROM (SELECT nvl(o.object_type, n.object_type) object_type
              FROM old_project o,
                   new_project n   
              where  o.object_id = n.object_id(+)
              UNION
              SELECT nvl(o.object_type, n.object_type) object_type
              FROM old_project o,
                   new_project n   
              where  o.object_id(+) = n.object_id) src,
           test_supported_objects t
    WHERE src.object_type = t.object_type(+);but there shouldn't be any restrictions on mixing them, right?

  • [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.

  • Can we join 3 tables with left outer join

    hi friends.
          Can any one tell how to join 3 tables using <b>left outer join</b>...i tried by taking some fields of <b>ekko,ekpo,eket</b>, but this not working ... plz give a sample code with some fields.

    Hi Uday
    <b>Inner joins using 3 tables </b>
    <i><b>Try this :-</b></i>
    SELECT stpo~stlnr stpo~idnrk mast~matnr mara~mtart stpo~menge 
    INTO CORRESPONDING FIELDS OF TABLE zmat1 FROM mast 
    JOIN stpo ON stpo~stlnr = mast~stlnr 
    JOIN mara ON mara~matnr = mast~matnr 
    WHERE stpo~stlty = 'M' "AND stpo~idnrk IN s_matnr 
    AND mast~werks = 1000.
    <b><i>Here s_matnr is a select-options on the selection-screen. </i></b>
    <i><b>Or this. </b></i>
    <b>Code: </b>
         Select single Vbrk~Bukrs Vbrk~Kunrg    Vbrk~Vbeln 
                       Vbrk~Fkdat Vbrk~Bstnk_Vf Vbrk~Zterm 
                       Tvzbt~Vtext 
                       Vbak~Vbeln Vbak~Bstdk 
                       Likp~Vbeln Likp~lfdat    Likp~Lfuhr 
           into w_vbrk 
           from vbrk 
          inner join       Tvzbt on Tvzbt~Zterm        = Vbrk~Zterm      and 
                                    Tvzbt~Spras        = sy-langu 
          Inner join       Vbfa  as SalesLnk 
                                 on SalesLnk~vbeln     = pu_vbeln        and 
                                    SalesLnk~vbtyp_v   = c_order 
                inner join Vbak  on Vbak~Vbeln           = SalesLnk~Vbelv
          Inner join       Vbfa  as DeliveryLnk 
                                 on DeliveryLnk~vbeln   = pu_vbeln       and 
                                    DeliveryLnk~vbtyp_v = c_Delivery 
                inner join Likp  on Likp~Vbeln          = DeliveryLnk~Vbelv 
          where vbrk~vbeln = pu_Vbeln.
    <i><b>This code locates sales, delivery and payment terms info from a billing document number.</b></i> 
    <i><b>or</b></i>
    <b>
    Here, this one also works fine :</b>
    select zfpcd~cadivi zfpcd~proforma zfpcd~factura zfpcd~aniofactura 
    zfpcd~montousd zfpcd~montoap zfpcd~ebeln zfpcd~inco1 
    zfpcd~lifnr lfa1~name1 zcdvs~status zfpcd~conint 
    into it_lista 
    from zfpcd inner join zcdvs 
    on zfpcd~ebeln = zcdvs~ebeln 
    and zfpcd~proforma = zcdvs~proforma 
    and zfpcd~lifnr = zcdvs~lifnr 
    inner join lfa1 
    on zfpcd~lifnr = lfa1~lifnr 
    where zcdvs~status = '04'.
    Reward if helpfull
    Regards
    Pavan

Maybe you are looking for