Doubt with OUTER JOIN behavior

I thought I knew all about OUTER JOINS to this scenario. Can pls someone explain how Oracle behaves in the following SQL code:

WITH t1 AS
(SELECT 1 as empid from dual
union all
select 2 from dual),
t2 as
(select 1 as empid from dual)
select *
from t1 
left outer join
t2
on (
t1.empid = t2.empid AND t1.empid=2
);

Expected O/P

EMPID EMPID_1
2NULL VALUE

Real O/P

EMPID EMPID_1
1NULL VALUE
2NULL VALUE

In this way...

SQL > ed
A written file afiedt.buf

1 t1 WITH (SELECT empid from all double union 1
2. Select 2 double),
3 t2 (select 1 as double empid)
4  --
5 Select
6 T1
7 * a left outer join t2 (t1.empid = t2.empid) and t1.empid = 2
SQL > /.

EMPID EMPID
---------- ----------
2
1

the "t1.empid = 2' is part of the OUTER JOIN condition.

In this way...

SQL > ed
A written file afiedt.buf

1 t1 WITH (SELECT empid from all double union 1
2. Select 2 double),
3 t2 (select 1 as double empid)
4  --
5 Select
6 T1
left outer join 7 t2 on (t1.empid = t2.empid)
8 * where t1.empid = 2
SQL > /.

EMPID EMPID
---------- ----------
2

the "t1.empid = 2" belongs to the filtering conditions

When it is part of the OUTER JOIN condition, he says that he should join t2 records where all conditions are met, otherwise the join should not be made, it does not say it to filter the overall results.

Maybe this helps clarify...

SQL > ed
A written file afiedt.buf

1 t1 WITH (SELECT empid from all double union 1
2. Select 2 double),
3 t2 (select empid Union 1 double all the)
4. Select 2 of the double)
5  --
6 select *.
7 T1
8 * a left outer join t2 (t1.empid = t2.empid) and t1.empid = 2
SQL > /.

EMPID EMPID
---------- ----------
2          2
1

Here we have 1 and 2 records in both tables, but the records in t2 are joined only where the record of t1 has an empid 2, so, although t2 has an empid 1, which is not reached because the join condition is not met.

Tags: Database

Similar Questions

  • BAD RESULTS WITH OUTER JOINS AND TABLES WITH A CHECK CONSTRAINT

    HII All,
    Could any such a me when we encounter this bug? Please help me with a simple example so that I can search for them in my PB.


    Bug:-8447623

    Bug / / Desc: BAD RESULTS WITH OUTER JOINS AND TABLES WITH a CHECK CONSTRAINT


    I ran the outer joins with check queries constraint 11G 11.1.0.7.0 and 10 g 2, but the result is the same. Need to know the scenario where I will face this bug of your experts and people who have already experienced this bug.


    Version: -.
    SQL> select * from v$version;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0      Production
    TNS for Solaris: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production

    Why do you not use the description of the bug test case in Metalink (we obviously can't post it here because it would violate the copyright of Metalink)? Your test case is not a candidate for the elimination of the join, so he did not have the bug.

    Have you really read the description of the bug in Metalink rather than just looking at the title of the bug? The bug itself is quite clear that a query plan that involves the elimination of the join is a necessary condition. The title of bug nothing will never tell the whole story.

    If you try to work through a few tens of thousands of bugs in 11.1.0.7, of which many are not published, trying to determine whether your application would be affected by the bug? Wouldn't be order of magnitude easier to upgrade the application to 11.1.0.7 in a test environment and test the application to see what, if anything, breaks? Understand that the vast majority of the problems that people experience during an upgrade are not the result of bugs - they are the result of changes in behaviour documented as changes in query plans. And among those who encounter bugs, a relatively large fraction of the new variety. Even if you have completed the Herculean task of verifying each bug on your code base, which would not significantly easier upgrade. In addition, at the time wherever you actually performed this analysis, Oracle reportedly released 3 or 4 new versions.

    And at this stage would be unwise to consider an upgrade to 11.2?

    Justin

  • doubt left outer join

    Hi all

    I use under version

    Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0

    SQL > SELECT E.ENAME,.

    2 D.DEPTNO,

    3 D.LOC

    4. TO EMP E,.

    DEPT 5 D

    6. WHERE = E.DEPTNO D.DEPTNO (+);

    ENAME, DEPTNO LOC

    ---------- ------ -------------

    DALLAS SMITH 20

    ALLEN 30 CHICAGO

    WARD 30 CHICAGO

    20 DALLAS JONES

    MARTIN 30 CHICAGO

    BLAKE 30 CHICAGO

    CLARK 10 NEW YORK

    SCOTT 20 DALLAS

    THE 10 NEW YORK KING

    TURNER 30 CHICAGO

    20 DALLAS ADAMS

    JAMES 30 CHICAGO

    FORD 20 DALLAS

    MILLER 10 NEW YORK

    40 BOSTON

    15 selected lines

    -----------------------------------------------------------------------------------------------------------------------------

    SQL > SELECT E.ENAME,.

    2 D.DEPTNO,

    3 D.LOC

    4. TO EMP E

    5 LEFT OUTER JOIN

    D 6 DEPT

    7. THE E.DEPTNO = D.DEPTNO;

    ENAME, DEPTNO LOC

    ---------- ------ -------------

    MILLER 10 NEW YORK

    THE 10 NEW YORK KING

    CLARK 10 NEW YORK

    FORD 20 DALLAS

    20 DALLAS ADAMS

    SCOTT 20 DALLAS

    20 DALLAS JONES

    DALLAS SMITH 20

    JAMES 30 CHICAGO

    TURNER 30 CHICAGO

    BLAKE 30 CHICAGO

    MARTIN 30 CHICAGO

    WARD 30 CHICAGO

    ALLEN 30 CHICAGO

    14 selected lines

    My doubt is both are same query is the same, is in ansi format and is in the format of the Oracle,.

    but the results are different.

    For the first query null is coming for unmatched records in the dept table

    but in the second query, it does not come

    Thank you

    Hello

    2947022 wrote:

    Hi all

    I use under version

    Connected to Oracle Database 11g Express Edition Release 11.2.0.2.0

    SQL > SELECT E.ENAME,.

    2 D.DEPTNO,

    3 D.LOC

    4. TO EMP E,.

    DEPT 5 D

    6. WHERE = E.DEPTNO D.DEPTNO (+);

    ENAME, DEPTNO LOC

    ---------- ------ -------------

    DALLAS SMITH 20

    ALLEN 30 CHICAGO

    WARD 30 CHICAGO

    20 DALLAS JONES

    MARTIN 30 CHICAGO

    BLAKE 30 CHICAGO

    CLARK 10 NEW YORK

    SCOTT 20 DALLAS

    THE 10 NEW YORK KING

    TURNER 30 CHICAGO

    20 DALLAS ADAMS

    JAMES 30 CHICAGO

    FORD 20 DALLAS

    MILLER 10 NEW YORK

    40 BOSTON

    15 selected lines

    -----------------------------------------------------------------------------------------------------------------------------

    SQL > SELECT E.ENAME,.

    2 D.DEPTNO,

    3 D.LOC

    4. TO EMP E

    5 LEFT OUTER JOIN

    D 6 DEPT

    7. THE E.DEPTNO = D.DEPTNO;

    ENAME, DEPTNO LOC

    ---------- ------ -------------

    MILLER 10 NEW YORK

    THE 10 NEW YORK KING

    CLARK 10 NEW YORK

    FORD 20 DALLAS

    20 DALLAS ADAMS

    SCOTT 20 DALLAS

    20 DALLAS JONES

    DALLAS SMITH 20

    JAMES 30 CHICAGO

    TURNER 30 CHICAGO

    BLAKE 30 CHICAGO

    MARTIN 30 CHICAGO

    WARD 30 CHICAGO

    ALLEN 30 CHICAGO

    14 selected lines

    My doubt is both are same query is the same, is in ansi format and is in the format of the Oracle,.

    but the results are different.

    For the first query null is coming for unmatched records in the dept table

    but in the second query, it does not come

    Thank you

    In fact, these requests are not the same.

    The first is to find all the lines of the Department, with the corresponding lines of PGE (when there are).  This is equivalent to «FROM dept LEFT OUTER JOIN emp...» ».

    The second is to find all the rows in the emp of the lines of the Department (when there are any).  This is equivalent to «...» WHERE e.deptno = d.deptno (+).

  • How to manage the update/insert in display with Outer Join object?

    Hello

    I have a problem in the treatment of update/insert in the original Version that contains two EOs with right outer join. The first EO values are inserted before and I want if second values EO already exists, it will update and if not a new record created.

    Error when I commit after entering values is: ' entity line with null key is not found in SecondEO. "

    What is the solution?

    Thank you

    Hello

    Make sure that your view object, you have included the key attributes of the two entity objects.

    Kind regards

    Saif Khan.

  • Max() with outer join

    I have a couple of tables XX_PERS and XX_ADDRESS as shown below. I would like to return more recent 'city' for each person on the xx_pers table. If no record matching in xx_address, there should always be the id of the person, name of city as null.

    CREATE TABLE xx_pers

    (Key primary ID number,)

    name varchar2 (100)

    );

    CREATE TABLE xx_address

    (

    addr_id PRIMARY KEY NUMBER,

    pers_id NUMBER,

    City VARCHAR2 (25).

    FOREIGN KEY (pers_id) REFERENCES xx_pers (id)

    );

    insert into xx_pers values (1, 'Messi');

    insert into xx_pers values (2, 'Muller');

    insert into xx_pers values (3, 'Ronaldo');

    insert into xx_address values (100.1, "Argentina");

    insert into xx_address values (102.1, "Madrid");

    insert into xx_address values (101.2, "Germany");

    insert into xx_address values (103.2, "Munich - OF");

    The following query returns the most recent 'city' for person 1 and 2 but do not know how to get the 3rd person.

    SELECT ID, name, city

    of xx_pers xx

    , xx_address y

    Where xx.id = y.pers_id

    and y.addr_id in (select max (addr_id) of the xx_address by pers_id group);

    Hello

    Here is another way, which is similar to what you have tried initially, but it uses an outer join to a subquery instead of an IN subquery:

    WITH got_max_addr_id AS

    (

    SELECT pers_id

    MAX (addr_id) AS max_addr_id

    OF xx_address

    GROUP BY pers_id

    )

    SELECT p.id

    p.name

    a.city

    OF xx_pers p

    LEFT OUTER JOIN got_max_addr_id ON m.pers_id = p.id m

    A LEFT OUTER JOIN xx_address ON a.pers_id = m.pers_id

    AND a.addr_id = m.max_addr_id

    ORDER BY p.id

    ;

    It is also very similar to the solution in response #2 above.

    Note Although all 4 of these solutions something in common that is different from the query you posted: they use all of the outer joins.  You obviousy has estimated that an outer join outer join would be useful here, because you mentioned an outer join in the title of the thread, but you are not one yourself.

  • need help with outer join filter.

    Need a little help to filter a result set and I can't seem to find a good way to do this.
    /*table*/
    
    create table invoice( farinvc_invh_code varchar2(100),
                                  farinvc_item varchar2(100),
                                  farinvc_po varchar2(100)
                                  )
       create table po( 
            supplier_number varchar2(60),
            supplier_invoice_no varchar2(60),
            po_number varchar2(60),
            run_date varchar2(60),
            PO_LINE_NUMBER varchar2(60) );
    /*data*/
    
    INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '1', 'P0142245');
    INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO_ITEM) VALUES ('I0554164', '3', 'P0142245');
    INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '1', 'P0142246');
    INSERT INTO "INVOICE" (FARINVC_INVH_CODE, FARINVC_ITEM, FARINVC_PO) VALUES ('I0554165', '2', 'P0142246');
    
    
    
    
    
    INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529132260', 'P0142245', '21-NOV-12', '1');
    INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '3');
    INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100121', '529137831', 'P0142245', '21-NOV-12', '2');
    INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145678', 'P0142246', '22-NOV-12', '1');
    INSERT INTO "PO" (SUPPLIER_NUMBER, SUPPLIER_INVOICE_NO, PO_NUMBER, RUN_DATE, PO_LINE_NUMBER) VALUES ('914100122', '145679', 'P0142246', '22-NOV-12', '2');
    query execution of im.

    SELECT  farinvc_invh_code,
                    supplier_number,
                    supplier_invoice_no,
                    farinvc_item, 
                    farinvc_po ,
                    po_number,
                    run_date,
                    PO_LINE_NUMBER
            FROM INVOICE, PO
            WHERE PO_NUMBER = FARINVC_PO(+)
            AND FARINVC_ITEM(+) = PO_LINE_NUMBER
            
    result
    "FARINVC_INVH_CODE"           "SUPPLIER_NUMBER"             "SUPPLIER_INVOICE_NO"         "FARINVC_ITEM"                "FARINVC_PO"                  "PO_NUMBER"                   "RUN_DATE"                    "PO_LINE_NUMBER"              
    "I0554165"                    "914100122"                   "145678"                      "1"                           "P0142246"                    "P0142246"                    "22-NOV-12"                   "1"                           
    "I0554165"                    "914100122"                   "145679"                      "2"                           "P0142246"                    "P0142246"                    "22-NOV-12"                   "2"                           
    "I0554164"                    "914100121"                   "529132260"                   "1"                           "P0142245"                    "P0142245"                    "21-NOV-12"                   "1"                           
    "I0554164"                    "914100121"                   "529137831"                   "3"                           "P0142245"                    "P0142245"                    "21-NOV-12"                   "3"                           
    ""                            "914100121"                   "529137831"                   ""                            ""                            "P0142245"                    "21-NOV-12"                   "2"                           
    It is a much larger table, and I took an excerpt in order to keep things clear and understanding. I would like to filter the result set to only show lines that have po numbers are the same and line are the same but there is an additional element. in other words as such.
    "FARINVC_INVH_CODE"           "SUPPLIER_NUMBER"             "SUPPLIER_INVOICE_NO"         "FARINVC_ITEM"                "FARINVC_PO"                  "PO_NUMBER"                   "RUN_DATE"                    "PO_LINE_NUMBER"              
    "I0554164"                    "914100121"                   "529132260"                   "1"                           "P0142245"                    "P0142245"                    "21-NOV-12"                   "1"                           
    "I0554164"                    "914100121"                   "529137831"                   "3"                           "P0142245"                    "P0142245"                    "21-NOV-12"                   "3"                           
    ""                            "914100121"                   "529137831"                   ""                            ""                            "P0142245"                    "21-NOV-12"                   "2"                           

    Hello

    Let me assure you that I understand.
    Last time, we were looking for the PO_NUMBERs who have been partially put into correspondence , i.e. groups of rows in the order table with the same po_number, which some had corresponding lines in the table Bill, and some of whom have not. It was essential that there is at least 1 line and 1 row without connections with the same purchase order.
    Now that you are interested SUPPLIER_INVOICE_NOs who are partially paired, i.e. groups of rows in the table of po with the same po_number and supplier_invoice_no, some of which have corresponding lines in the invoice table, and some are not. ("Supplier_Invoice_No" is quite a mouthful. "We'll abbreviate as sin in the future.) However, the final selection is based on po_numbers: If a po_number has partially matched sins, then we are interested all po_number. For example, the result set must include = 529132260 SIN, even if that sin is completely, because there is a partially matching sin (529137831) with the same po_number (P0142245).

    As this problem revolves around partially matching sins, let's call them Cardinal sins . We can calculate match_cnt and total_cnt based NAS as well as po_number. Then, we can use another analytic function so see if the po_number has all cardinal_sins, like this:

    WITH    joined_data     AS
    (
         SELECT     i.farinvc_invh_code
         ,     p.supplier_number
         ,     p.supplier_invoice_no
         ,     i.farinvc_item
         ,     i.farinvc_po
         ,     p.po_number
         ,     p.run_date
         ,     p.po_line_number
         ,     COUNT (i.farinvc_po) OVER ( PARTITION BY  p.po_number
                                            ,                  p.supplier_invoice_no
                               )     AS match_cnt
         ,     COUNT (*)           OVER ( PARTITION BY  p.po_number
                                            ,                  p.supplier_invoice_no
                                          )     AS total_cnt
         FROM           po       p
         LEFT OUTER JOIN  invoice  i  ON   i.farinvc_po    = p.po_number
                                      AND  i.farinvc_item  = p.po_line_number
    )
    ,     got_cardinal_sin_cnt     AS
    (
         SELECT  joined_data.*
         ,     SUM ( CASE
                        WHEN  match_cnt  >= 1
                     AND     match_cnt  <  total_cnt
                     THEN  1
                    END
                  ) OVER (PARTITION BY  po_number)     AS cardinal_sin_cnt
         FROM    joined_data
    )
    SELECT    farinvc_invh_code
    ,       supplier_number
    ,       supplier_invoice_no
    ,       farinvc_item
    ,       farinvc_po
    ,       po_number
    ,       run_date
    ,       po_line_number
    FROM       got_cardinal_sin_cnt
    WHERE       cardinal_sin_cnt     > 0
    ORDER BY  po_number
    ,            farinvc_item
    ;
    
  • Doubts of outer join

    Hello
    I found a query with the clause where something like this
    from d_claim d, c_insgroup g
    where 
    A.INSCODE=G.INSCODE(+)
    and a.clntsys = g.clntsys
    AND G.SYSTEM(+) = '0'
    You asked to help me understand this.
    I thought of an outer join, I need place (+) in all the columns where we refer to the table G

    You thought that right.
    The example is an incorrect implementation of an outer join.

  • Interactive reports on views with outer joins.

    We tried to create an interactive report on a view with an outer join. It generated an error indicating that there is not a single column to identify the row. What APEX rule uses to identify the column that should be identify the line? Select statement should the correct column in the table that provides the extra lines to be one that is identified in the view?

    This is your link to the unique view of the record.

    Delete the link; make your view preserved key (so you can use rowid) or identify a column to uniquely identify these lines for Apex.

    This is done in the link column settings in the report attributes.

    SW

  • Need help with outer joins

    I have the following table structure,

    _ Table - 1
    ---------------------------------
    ID | Information
    ---------------------------------
    1. abcadskasasa
    2. asdasdasdasd
    3. saeqdfdvsfcsc
    ---------------------------------


    _ Table - 2
    ---------------------------------
    ID | NEST
    ---------------------------------
    1. 12
    1. 13
    2. 14
    1. 15
    1. 16
    2. 12
    ---------------------------------



    _ Table - 3
    ---------------------------------
    ID | THIERRY
    ---------------------------------
    1. 12
    2. 14
    1. 15
    ---------------------------------

    Now, I want to choose for each ID in table 1 and the number of MIP in the table 2-number of THIERRY of table 3.

    Desired output:_

    ---------------------------------------------------------------------------------------------------
    ID | COUNT_PID | COUNT_PARID
    ---------------------------------------------------------------------------------------------------
    1. 4. 2
    2. 2. 1
    3. 0 | 0
    ---------------------------------------------------------------------------------------------------

    Could someone please help me with this. I'm doing using outer joins, but as I work mainly at the edge of the end, not able to reach an appropriate solution to that above.

    Thanks in advance,
    Tejas

    You should not outer join... That should do it...

    select ID , (select count(PID) from table2  t2 where t2.id = t1.id) , (select count(PARID) from table3  t3 where t3.id = t1.id)
    from table1
    
  • Problem with Outer join and filter

    Hello

    I join two tables in the source using a left outer join. Outside of the join, I have a filter specified with condition TabA.C1 > TabB.C2.

    Now, when ODI generates the query it puts the left outer join on the filter condition as well. So he puts filter as

    where
    (1 = 1)
    And ((TabA.C1 = TabB.C1 (+)) AND)
    (TabA.C2 = TabB.C2 (+))
    And TabA.C10 > TabB.C14 (+)

    How to avoid this problem. I tried this performance on stage as well, always generated query remains the same.

    I use the incremental update of the IKM Oracle. My source and target are both on the same PB.


    ~ Chikk

    Hi Chikk,

    If you analyze the data, you'll see it's OK to have the "(+)" to the filter...

    Anyway, if you want to drop it, leave it as inner join and put the "(+)" manually to the join object.

    This help you?

  • Update with Outer Join, round 2

    Thanks for those of you who have helped me out on the first (I never thought that you could use a SELECTION of a line like that).

    However, here is a new version of my problem:

    I have three tables.
    Table_1 has a column that must be updated based on the Table_2 and Table_3 values.
    Table_1 both Table_2 have values used to determine which line of Table_3 to use.
    However, not all the rows in Table_1 has a corresponding line of Table_3, in which case the value of Table_3 to use is assumed to be 1.

    The tables and the corresponding columns are:

    TABLE_1
    value_1 - value update
    key_2 - a pointer to TABLE_2
    key_3a - a pointer to a TABLE_3 or a dummy value if there is no record of the TABLE_3

    TABLE_2
    key_2 - the primary key
    key_3b - a secondary pointer to TABLE_3
    Value_2 - a value to use in the calculation of TABLE_1.value_1

    TABLE_3
    key_3a - the first part of the unique key
    ley_3b - the second part of the unique key
    value_3 - a value to use in the calculation of TABLE_1.value_1

    If there is a line in table_3 which matches the values table_1.key_3a and table_2.key_3b (where table_2.key_2 = table_1.key_2):
    Set table_1.value_1 = table_2.value_2 * table_3.value_3
    If there is no such line in table_3:
    Set table_1.value_1 = table_2.value_2

    I want to do something like this:

    UPDATE table_1 t1
    SET = Value_1
    (
    SELECT T2.value_2 * NVL (t3.value_3, 1)
    IN table_2 t2
    LEFT JOIN t3 table_3
    WE (t3.key_3b = t2.key_3b and t3.key_3a = t1.key_3a)
    WHERE t2.key_2 = t1.key_2
    )

    However, Oracle does not t1 to be referenced in the outer join clause.
    (Assume that each value of key_2 in table_1 is table_2 as well: it is only the key_3 value that can be a model.)

    If I move "t3.key_3 = t1.key_3" to the WHERE clause, so t1.value_1 is null for lines without the corresponding value of the table_3.

    I can do it with a clone of table_1 using ROWID:

    UPDATE table_1 t1
    SET = Value_1
    (
    SELECT T2.value_2 * NVL (t3.value_3, 1)
    FROM table_1 t1a
    JOIN the t2 table_2
    ON t2.key_2 = t1a.key_2
    LEFT JOIN t3 table_3
    WE (t3.key_3b = t2.key_3b and t3.key_3a = t1a.key_3a)
    WHERE t1a.row_id = t1.row_id
    )

    However, is there an easier way to do it using ANSI joins (i.e. without (+) syntax)?
    I have this feeling I'm missing something obvious here.

    Sorry, I'm not sure understand your scenario this time. It is early and I did have my 64 ounces of caffiene still.

    I didn't look at the statement, you were trying to run and reformatting:

    SQL> UPDATE table_1 t1
      2  SET    value_1 = (SELECT t2.value_2 * NVL((select t3.value_3
      3                                             from   table_3 t3
      4                                             where  t3.key_3b = t2.key_3b
      5                                             and    t3.key_3a = t1.key_3a)
      6                                          ,1)
      7                    FROM   table_2 t2
      8                    WHERE  t2.key_2 = t1.key_2
      9                   )
     10  ;
    
    0 rows updated.
    

    I don't know if it will work for you, but at least it is syntactically correct.

  • Update with Outer Join

    Is it possible to do an update that involves an outer join on the updated table?

    Here's what I mean - now, I have something like:

    UPDATE table_1 t1
    SET col_1 =
    (
    SELECT t2.col_2
    IN table_2 t2
    WHERE t2.t1_key = t1.t1_key
    )
    WHERE THERE ARE
    (
    SELECT t2.*
    IN table_2 t2
    WHERE t2.t1_key = t1.t1_key
    );
    --
    UPDATE table_1 t1
    SET col_1 = 0
    WHERE THERE IS NO
    (
    SELECT t2.*
    IN table_2 t2
    WHERE t2.t1_key = t1.t1_key
    );

    Yes, I could set all values of table_1.col_1 = 0 first and then perform the update first, but it is inefficient because of the number of records in the table that could be updated twice.

    Is it possible to combine these two updates in a single update statement?

    You can simply use your first update and omit the WHERE EXISTS clause since you want to update all rows in table_1 anyway.

    If the subquery finds a match, it will update the selected value. Normally, a non-match would set the column to a null value, but you can solve this with NVL:

    SQL> select * from table_1;
    
                  T1_KEY                COL_1
    -------------------- --------------------
                       1                    1
                       2                    1
                       3                    1
                       4                    1
                       5                    1
                       6                    1
                       7                    1
                       8                    1
                       9                    1
    
    9 rows selected.
    
    SQL> select * from table_2;
    
                  T2_KEY                COL_2
    -------------------- --------------------
                       1                    9
                       3                    9
                       5                    9
    
    SQL> UPDATE table_1 t1
      2  SET    col_1 = nvl (
      3                       (SELECT t2.col_2
      4                        FROM   table_2 t2
      5                        WHERE  t2.t2_key = t1.t1_key
      6                       ),0
      7                     )
      8  ;
    
    9 rows updated.
    
    SQL> select * from table_1;
    
                  T1_KEY                COL_1
    -------------------- --------------------
                       1                    9
                       2                    0
                       3                    9
                       4                    0
                       5                    9
                       6                    0
                       7                    0
                       8                    0
                       9                    0
    
    9 rows selected.
    
  • Need help with outer join

    Hello

    I have a requirement in which I need to get data from a third table where a date of the third table is higher than a date in the second array.

    Ex:

    SELECT t1.column1, t3.column2
    FROM t1, t2, t3
    WHERE t1.id = t2.foreign_id
    AND t1.id ( + ) = t3.foreign_id
    AND t3.some_date_column > t2.another_date_column
    
    

    However, using the query above returns no results if the date condition is not met. I still need to show t1.column1 and a null t3.column2.

    How should I do this?

    Thank you

    Allen

    Edit: Added information about the requirement.

    Hi Allen

    1. SELECT t1.column1, t3.column2
    2. T1, t2, t3
    3. WHERE t1.id = t2.foreign_id
    4. AND t1.id = t3.foreign_id (+)
    5. AND t3.some_date_column (+) > t2.another_date_column

    I guess that this t1.column1 must not be null. Or am I wrong? The + sign must be placed on the side where draws are accepted. You must repeat it for each condition on the table.

    Alternativlely you can use the LEFT OUT JOIN syntax. If the two columns are allowed null you need a FULL OUTER JOIN.

    BTW: The join to t2 is not required if a refefernce constraint forced.

  • restrict records with outer join

    Hello

    With the help of Oracle 11 g R2.

    I have 2 tables and I want outside to join them. However, when a record is found on the 2nd table, I only need the folder with the largest value in column 2. Here are some data.

    create table a (col1, col2 number)
    insert into a values (1, 10)
    insert into a values (1, 20)
    insert into a values (1, 30)
    insert into a values (1, 30)
    insert into a values (2: 20)
    insert into a values (3, 10)
    insert into a values (3: 20)
    insert into a values (3, 30)

    create table b (col1, col2 number number)
    insert into values of b (1, 1)
    insert into values of b (1, 2)
    insert into values of b (2, 1)
    insert into values of b (2, 2)

    This shows all the records, but when a record is found in table b, I need the folder with the largest value for column 2.

    select a.col1, 
           a.col2, 
           b.col1, 
           b.col2
    from   a, b
    where  a.col1 = b.col1 (+)
    and    a.col2 = 20
    order by 1, 2
    

    I tried this, but too many files got removed the result.

    
    select a.col1, 
           a.col2, 
           b.col1, 
           b.col2
    from   a, b
    where  a.col1 = b.col1 (+)
    and    a.col2 = 20
    and    b.col2 = (select max(b2.col2) from b b2
                     where  b2.col1 = b.col1)
    order by 1, 2
    

    Hello

    The General method to do this is to treat it as a Query of Top - N, where you choose N (N = 1 in this case) lines from the top of an ordered list:

    WITH got_r_num AS

    (

    Select a.col1,

    a.col2,

    b.Col1 AS b_col1,

    b.col2 AS b_col2

    ROW_NUMBER () OVER (PARTITION BY a.col1

    ORDER BY b.col2 DESC

    ) AS r_num

    of a, b

    where a.col1 = b.col1

    and a.col2 = 20

    )

    SELECT col1, col2, b_col1, b_col2

    OF got_r_num

    WHERE r_num = 1

    ORDER BY col1, col2

    ;

    In this case, you could also do a GROUP BY:

    Select a.col1,

    a.col2,

    b.Col1,

    MAX (b.col2) AS b_col2

    of a, b

    where a.col1 = b.col1

    and a.col2 = 20

    GROUP OF a.col1, a.col2, b.col1

    order by 1, 2

    ;

  • COUNT with outer join!

    Hello:
    I have the following script:
    SELECT   d.dname, COUNT (e.empno) COUNT
        FROM emp e, dept d
       WHERE e.deptno(+) = d.deptno
    GROUP BY d.dname
    ORDER BY 1
    and the output is:
    DNAME _COUNTY
    ACCOUNTING _ 3
    OPERATIONS _ 0
    SEARCH _ 5
    SALE _ 6

    and if I changed the script to be:
    SELECT   d.dname, COUNT (e.empno) COUNT
        FROM emp e, dept d
       WHERE e.deptno(+) = d.deptno AND sal > 3000
    GROUP BY d.dname
    ORDER BY 1
    The output will be:
    DNAME _COUNTY
    ACCOUNTING _ 3

    Now the question, what happens if I want to include all departments with County = 0? I mean:
    DNAME _COUNTY
    ACCOUNTING _ 3
    OPERATIONS _ 0
    SEARCH _ 0
    SALE _ 0

    Saad,

    Published by: S.Nayef on April 14, 2010 10:56 (training)

    Hello

    It would be something like this:

    select dj.dname,dj.level_no,count(e.empno) count
    from
    (select d.dname,j.level_no,j.job_name,d.deptno
    from dept d, job j
    ) dj
    , emp e
    where dj.job_name = e.job(+)
    and    e.deptno(+) = dj.deptno
    and    e.sal(+) > 3000
    group by dj.dname, dj.level_no
    order by 1,2
    

    Hope this helps

    Herald tiomela
    Superconsult.nl

Maybe you are looking for

  • CD player is not found code 39

    I lost the way to use the CD player, I have CD ROM recovery, but these cannot be read I have XP Professional installed any help on how to change the Setup program to install the CD player again on my Satellite Pro 4300

  • Y570 Windows 7 reinstall errors

    I had my Y570 for some time now and recently decided a clean install and update all the drivers and all. I got a copy of ISO of Windows 7 HP and was planning on the use of the series on the back. I created my bootable DVD, and when I entered the faci

  • Do not install driver for media center M7000

    Hi all: for the first time here. Full of hope! Recently repaired a system Media Center M7000. Was able to get most of the pilots of the HP excelllent site. However, the audio driver listed when downloaded and loaded into the system just didn't work.

  • AAA for switch Cat OS

    Hello I had a problem on the creation of a Shell command authorization for my cat OS switches. My GBA version is 3.3 Help you enjoy Thank you Jong

  • Laptop shows "no speakers or headphones are connected.

    I tried to figure this out for a while. So far, it is where I am. I can't get my controller high definition audio and standard Open hcd usb host controller to install drivers. Windows is not a complete update. Additional details: I have a Gateway lap