problems with natural join

Hello!

I learn SQL and I have a lot of doubts but I think with this example I can generalize.

I have the tables:

Book {idbook (PK), namebook}
auhor {idauthor (PK), nameauthor, idcountry (FK)}
fatherhood {idauthor (FK), idbook (FK)} (both the same strain of PK)
country {idcountry (PK), namecountry}

I want the name of the books that are the authors of the Canada.

I assumed that a correct query would be:

SELECT namebook FROM book NATURAL JOIN (paternity NATURAL JOIN (author country NATURAL JOIN)) WHERE country.namecountry = 'Canada ';

The result I was waiting for was:

Book3
Book5

but this query returns me all the books that have relationships in the co-author (with the authors of all countries), 2 times! as:

Book2
book3
Book4
Book5
Book2
book3
Book4
Book5

the best I can do to get my correct result is:

SELECT namebook FROM book NATURAL JOIN (author, NATURAL JOIN) WHERE author.idcountry = 2;

But of course I can't use this one...

Can someone explain to me what is happening?

Thank you very much!

Published by: user12040235 on 10/15/2009 09:37

Published by: user12040235 on 10/15/2009 09:51

Hello

Maybe it's a bug.

Get the correct results (2 ranks) to Oracle 10.1, but I get the same bad results you (12 rows) in Oracle 11.1.
In Oracle 11, I get results so say "SELECT *" instead of "SELECT book.namebook".
I also get the correct results, if I add the join columns in the SELECT clause. Adding a column to join no, for example

SELECT  BOOK.namebook, author.nameauthor
FROM            book
...

Gets results.

In the interest of all those who want to try this:

DROP TABLE     author;

create table     author     AS
SELECT  1 AS idauthor, 'Jose Luiz do Rego' AS nameauthor, 1 AS idcountry     FROM dual     UNION ALL
SELECT         2, 'Barbara Bela',                                      2          FROM dual     UNION ALL
SELECT         3, 'Juan Domingues',                                    5          FROM dual     UNION ALL
SELECT         4, 'José Mauro de Vasconcelos',                         1          FROM dual     UNION ALL
SELECT         5, 'Vader',                                             2          FROM dual     UNION ALL
SELECT         6, 'navathe',                                           4          FROM dual     UNION ALL
SELECT         7, 'Machado de Assis',                                  1          FROM dual
;

drop table     AUTHORSHIP;

CREATE TABLE     authorship     AS
SELECT         2 AS idauthor,          5 AS idbook     FROM dual     UNION ALL
SELECT         1 AS idauthor,          1 AS idbook     FROM dual     UNION ALL
SELECT         5 AS idauthor,          3 AS idbook     FROM dual     UNION ALL
SELECT         6 AS idauthor,          2 AS idbook     FROM dual     UNION ALL
SELECT         7 AS idauthor,          4 AS idbook     FROM dual     UNION ALL
SELECT         7 AS idauthor,          6 AS idbook     FROM dual;

drop table     book;

CREATE TABLE   book     AS
SELECT         1 AS idbook, 'book1' AS namebook     FROM dual     UNION ALL
SELECT         2 AS idbook, 'book2' AS namebook     FROM dual     UNION ALL
SELECT         3 AS idbook, 'book3' AS namebook     FROM dual     UNION ALL
SELECT         4 AS idbook, 'book4' AS namebook     FROM dual     UNION ALL
SELECT         5 AS idbook, 'book5' AS namebook     FROM dual     UNION ALL
SELECT         6 AS idbook, 'book6' AS namebook     FROM dual     UNION ALL
SELECT         7 AS idbook, 'book7' AS namebook     FROM dual;

DROP TABLE     country;

CREATE TABLE     country     AS
SELECT         1 AS idcountry, 'Brazil' as namecountry     FROM dual     UNION ALL
SELECT         2 AS idcountry, 'Canada' as namecountry     FROM dual     UNION ALL
SELECT         3 AS idcountry, 'Chile' as namecountry     FROM dual     UNION ALL
SELECT         4 AS idcountry, 'Venezuela' as namecountry    FROM dual     UNION ALL
SELECT         5 AS idcountry, 'USA' as namecountry          FROM dual     UNION ALL
SELECT         6 AS idcountry, 'Argentina' as namecountry    FROM dual     ;

Tags: Database

Similar Questions

  • Beginner help - faced with natural join as an example

    Hello

    My apologies for how basic this query probably will, but I am teaching from zero and do not like to move from topic to topic without a good understanding of why I get the results I get, rather than knowing 'it's how he did

    I use the guide of the official examination for 1z0 - 051, and 320 page there is the following year:

    + "The table JOB_HISTORY sharing three columns with the same named with the EMPLOYEES table: EMPLOYEE_ID JOB_ID and department_id." You are required to describe the tables and fetch the EMPLOYEE_ID, JOB_ID, +.
    + Values for all rows retrieved using a natural join pure department_id, LAST_NAME, HIRE_DATE and end_date. Alias the table EMPLOYEES as EMP and the table JOB_HISTORY that JH and use dot notation where possible. » +


    Their solution (which is about what I came to start with) is:

    SELECT employee_id job_id, department_id, emp.last_name, emp.hire_date, JH.end_date
    OF JH job_history
    NATURAL JOIN employees emp;

    This translates into the only employee "Taylor" being returned.

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

    Is it possible someone could 'hold my hand' through this example please? I have no idea why only one result is returned, but in addition, I don't know what else I expected - clearly my understanding of the natural join clause is severely lacking. The book States that:

    + "The execution of this statement returns a single row with the same values EMPLOYEE_ID JOB_ID and department_id in the two tables." +

    I guess I'm confused because I thought that the join clauses should deploy additional content to other tables to display, to not limit lines - which is what it sounds like they mean by "returns a single row with the same number and job_id department_id values in the two tables."



    I am very confused!

    Thanks in advance,

    Nick

    Hi, Nick.

    Nick woodward wrote:
    Hello

    My apologies for how basic this query probably will, but I am teaching from zero and do not like to move from topic to topic without a good understanding of why I get the results I get, rather than knowing 'it's how he did

    Good thinking!

    I use the guide of the official examination for 1z0 - 051, and 320 page there is the following year:

    + "The table JOB_HISTORY sharing three columns with the same named with the EMPLOYEES table: EMPLOYEE_ID JOB_ID and department_id." You are required to describe the tables and fetch the EMPLOYEE_ID, JOB_ID, +.
    + Values for all rows retrieved using a natural join pure department_id, LAST_NAME, HIRE_DATE and end_date. Alias the table EMPLOYEES as EMP and the table JOB_HISTORY that JH and use dot notation where possible. » +

    Their solution (which is about what I came to start with) is:

    SELECT employee_id job_id, department_id, emp.last_name, emp.hire_date, JH.end_date
    OF JH job_history
    NATURAL JOIN employees emp;

    This translates into the only employee "Taylor" being returned.

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

    Is it possible someone could 'hold my hand' through this example please? I have no idea why the only one result is returned,

    This is because there is only one combination of rows from the tables emp and jh such as the join condition is TRUE. In a natural join, the join condition is not specified, so it would be better if you started looking at the equivalent natural join:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    ,       emp.hire_date
    ,       jh.end_date
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
                        AND  emp.job_id          = jh.job_id
                        AND  emp.department_id  = jh.department_id
    ;
    

    on the other hand I don't know what I was expecting - clearly my understanding of the natural join clause is severely lacking. The book States that:

    + "The execution of this statement returns a single row with the same values EMPLOYEE_ID JOB_ID and department_id in the two tables." +

    I guess I'm confused because I thought that the join clauses should deploy additional content to other tables to display, do not limit the rows...

    A join does both. Each row in the result set can have columns (or drift) the columns in the table and it can have any row of a table associated with the lines of all (or part) of the other table. There are 107 lines in the emp table and 10 rows in the table of jh. This means that could result in a join 2 tables can 0-107 * 10 = 1070 lines. If the join conditions are so strict that no combination of lines met, then the result set has 0 rows. However, if they are so loose that all combinations of lines 2 brands joins tables condition TRUE, then each of the 107 rows in the emp table will join each of the 10 lines in the table of jh.

    Try to play with the join condition and see what happens. For example, we will comment on 2 of the 3 parts of the join condition, so that the only condition on the left is "emp.employee_id = jh.employee_id". Also, we'll include a few other columns in the table of jh and no poster not dates, just to make room for the new coplumns. So, if we execute this query:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    -- ,       emp.hire_date          -- Commented out to save space
    -- ,       jh.end_date          -- Commented out to save space
    ,        jh.job_id             -- For debugging
    ,      jh.department_id     -- For debugging
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
    --                    AND  emp.job_id          = jh.job_id
    --                    AND  emp.department_id  = jh.department_id
    ;
    

    then we get these results:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         101 AD_VP              90 Kochhar    AC_ACCOUNT        110
         101 AD_VP              90 Kochhar    AC_MGR            110
         102 AD_VP              90 De Haan    IT_PROG            60
         114 PU_MAN             30 Raphaely   ST_CLERK           50
         122 ST_MAN             50 Kaufling   ST_CLERK           50
         176 SA_REP             80 Taylor     SA_REP             80
         176 SA_REP             80 Taylor     SA_MAN             80
         200 AD_ASST            10 Whalen     AD_ASST            90
         200 AD_ASST            10 Whalen     AC_ACCOUNT         90
         201 MK_MAN             20 Hartstein  MK_REP             20
    
    10 rows selected.
    

    Why do we get only 10 lines? There are other employee_ids, such as 100 and 206 in the emp table. Why do we see these employee_ids in the results? In addition, the emp.employee_id is unique; in other words, no 2 rows in the emp table have the same employe_id. How the result set can have two lines with the same employee_ids, such as 101 or 200?

    Now try to do the more difficult condtion join, by a non-commenting on the terms that we have commented earlier:

    SELECT   emp.employee_id
    ,       emp.job_id
    ,       emp.department_id
    ,       emp.last_name
    -- ,       emp.hire_date
    -- ,       jh.end_date
    ,        jh.job_id
    ,      jh.department_id
    FROM      hr.job_history     jh
    JOIN       hr.employees      emp  ON   emp.employee_id    = jh.employee_id
                        AND  emp.job_id          = jh.job_id     -- *** CHANGED  ***
    --                    AND  emp.department_id  = jh.department_id
    ;
    

    The results are now:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         200 AD_ASST            10 Whalen     AD_ASST            90
         176 SA_REP             80 Taylor     SA_REP             80
    

    He was 2 of 10 lines in the previous result set. What happened to the other 8 lines that have been in this result set? Fior example, why has this line:

    EMPLOYEE            DEPARTMENT                       DEPARTMENT
         _ID JOB_ID            _ID LAST_NAME  JOB_ID            _ID
    -------- ---------- ---------- ---------- ---------- ----------
         176 SA_REP             80 Taylor     SA_MAN             80
    

    in the result of 10 rows value (produced by the query with a join condition only 1), but not in the result of 2 ranks (produced by the query with the additional condition 'AND emp.job_id = jh.job_id')?

    If you do not completely understand the natural joins, don't worry too much. In practice, nodody uses a natural join. However, natural joins are a type of INNER join and inner joins are extremely frequent. It is very important for you to understand how joins inside, such as the 2 I posted more top, work.

    Published by: Frank Kulash, March 18, 2013 17:08
    Results and applications added

  • Problem with the join relationship condition master / detail

    Hello
    I need to join with master detail such as if there is no trace of the foreign key in detail to the primary master key, the foreign key must be attached to another column based on certain conditions in the master. For example:

    Control columns: pk1, rk1, rk2
    Detail of the columns: fk1, a, b, c

    I created a relationship with detail with the join condition, pk1 = fk1 in master.
    But according to value rk1, I actually join fk1 to rk2 in master and if the condition fails then join the pk1. I can't put this condition in the relationship that the form does not accept complex join conditions.

    I hope that my example has not confused everyone.

    Can someone help me to achieve this goal.

    TIA

    TIA,
    I agree with DM, rather than using the RELATIONSHIP between the blocks that I would create logic to manually set the DEFAULT_WHERE clause on the block of RETAIL with the criteria you need to MASTER block. This can be a little difficult if you want to form work the same way if you had defined a relationship because you will have to create all the triggers event coordination. The most difficult part is to choose the right triggers to coordinate the synchronization when a user accesses a folder of master to another. For just the basics (the block of retail is questioned when the user navigates to the retail block), you could do the following:
    1. create a When-new-folder-Instance trigger on the Master block to capture the relevant details to query the block detail and define the DEFAULT_WHERE.
    2 create a trigger in a times - news - block - Instance on the block of retail to run the query on the block of retail and display the details records.

    For example:

    /* Sample When-New-Record-Instance trigger */
    DECLARE
         v_temp_where          VARCHAR2(500); /* make larger if needed. */
    BEGIN
         IF ( :MASTER_BLOCK.First_Item IS NOT NULL ) THEN
              /* this is needed for when the user navigates to a NEW record in the MASTER Block. */
    
              /* Add your conditional logic here to determine how to build your WHERE clause */
              IF ( RK1 = 'Dependent Values' ) THEN
                   v_temp_where := 'PK1 = FK1';
              ELSE
                   v_temp_where := 'RK2 = FK1';
              END IF;
    
              Set_Block_Property('DETAIL_BLOCK',ONETIME_WHERE,v_temp_where);
              /* If you are using Forms 6i, the ONETIME_WHERE property is not available so you will have to */
              /* use the DEFAULT_WHERE property instead. */
         END IF;
    END;
    
    /* Sample When-New-Block-Instance trigger
    BEGIN
         /* This sample code assumes the user has not navigated to a New Record */
         /* so there will always be a NOT NULL default where assigned to the DETAIL BLOCK.*/
         Clear_Block(ASK_COMMIT); /* just in case the user made changes... */\
         Execute_Query;
    END;
    

    Please understand it is the EXAMPLE of Code and is intended to be a starting point for you - not the complete solution.

    I hope this helps.
    Craig...

    If a response is appropriate or useful, please mark accordingly

  • 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?

  • Problem with a join query

    Having trouble getting my head around that, so would appreciate any help.

    I run the query below, which includes a join. If I run this query through Navicat (for those who don't know, a MySQL GUI) - I get the expected result.

    I also have a similar query running on the site elsewhere with success, so I am struggling to understand why it does not work.

    The request is;

    < name cfquery = "getwatched" datasource = "#datasource #" >
    SELECT r.retailer_name, r.id, r.shortname, d.id, d.merchant_id, d.user_id, d.e_updates
    OF cc_watchlists d
    LEFT JOIN merchant r
    ON d.merchant_id = r.id
    WHERE d.user_id = #SESSION. Auth.ID #.
    ORDER BY r.retailer_name ASC
    < / cfquery >

    and the exit;

    < cfoutput query = "getwatched" >
    < TD > < a href = "page.cfm? for = #shortname #" > #retailer_name # < /a > < /dt >
    < / cfoutput >

    Error States;

    coldfusion.runtime.UndefinedVariableException: Variable SHORTNAME is undefined.

    I would appreciate any help!

    Make a the entire query just before the line which is the slightest error, and
    Make sure that it contains what you think that it contains.

    --
    Adam

  • NATURAL JOIN problem

    Hello

    After reading a natural join on wikipedia , I was interested to use this syntax for expressing equi-joins between the tables that share some common column names. I have so these 3 tables:

    -Courses (acronym, title, nbCredits)
    -Registration (acronym, garnish, matricule, numSect, cumulative, noteFinale)
    -Nobody (SIN, name, typPers, service number, pmatricule)

    When I specify * in the select clause, I don't get that 3 lines, which is logical given the restriction I've specified in where clause. But what I don't understand is that if I replace the * with specific column names, then oracle returns a whole bunch of additional lines that should have been rejected in the where clause. It seems to me that Oracle should return only the next 3 rows. Does make sense to anyone? Here's the query I am running:

    SELECT *.
    OF course, NATURAL JOIN, NATURAL JOIN registration person
    WHERE name = 'John '.
    AND name = "Doe".
    AND nbCredits > 2
    AND noteFinale <>'F'
    AND noteFinale IS NOT NULL;

    -3 lines selected (right)

    Individual SELECTION, the title
    OF course, NATURAL JOIN, NATURAL JOIN registration person
    WHERE name = 'John '.
    AND name = "Doe".
    AND nbCredits > 2
    AND noteFinale <>'F'
    AND noteFinale IS NOT NULL;

    -80 (bad) selected lines

    Ludovic

    Published by: user3968717 on December 5, 2009 12:41

    Published by: user3968717 on December 5, 2009 12:42

    I can't reproduce the problem - I get 1 row in both cases

    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT sigle, titre
      2  FROM Cours NATURAL JOIN Inscription NATURAL JOIN Personne
      3  WHERE prenom='Pierre'
      4  AND nom='Daigneault'
      5  AND nbCredits>2
      6  AND noteFinale != 'F'
      7* AND noteFinale IS NOT NULL
    SQL> /
    
    SIGLE
    ------------
    TITRE
    --------------------------------------------------------------------------------
    
    CY130
    Clavardage et communications
    
    1 row selected.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT *
      2  FROM Cours NATURAL JOIN Inscription NATURAL JOIN Personne
      3  WHERE prenom='Pierre'
      4  AND nom='Daigneault'
      5  AND nbCredits>2
      6  AND noteFinale != 'F'
      7* AND noteFinale IS NOT NULL
    SQL> /
    
     MATRICULE SIGLE
    ---------- ------------
    TITRE
    --------------------------------------------------------------------------------
    
     NBCREDITS TRIM    NUMSECT  CUMULATIF NO        NAS
    ---------- ---- ---------- ---------- -- ----------
    NOM
    --------------------------------------------------------------------------------
    
    PRENOM
    --------------------------------------------------------------------------------
    
    T PMATRIC
    - -------
       1470609 CY130
    
    ...
    
     MATRICULE SIGLE
    ---------- ------------
    TITRE
    -------------------------------------------------------------------------------
    
     NBCREDITS TRIM    NUMSECT  CUMULATIF NO        NAS
    ---------- ---- ---------- ---------- -- ----------
    NOM
    -------------------------------------------------------------------------------
    
    PRENOM
    -------------------------------------------------------------------------------
    
    T PMATRIC
    - -------
    
    1 row selected.
    

    I use 11.1.0.7, what version are you using? He had a number of bugs with joins SQL 99 when they were introduced. Maybe you're using an older version? Did you ask the most recent Group of patches for your version of Oracle?

    I would caution, however, I would strongly, strongly suggest avoiding natural joins. In my mind, they are an abomination of SQL syntax. I love the syntax of SQL 99 and I'm perfectly happy with the syntax

    x JOIN y USING (column_name )
    

    or

    x JOIN y ON (x.column_name = y.column_name)
    

    But join based on all the identical column names causes a lot of problems in maintenance. With any other join syntax, if you si vous allez are going to add new columns to a table, the results of your SQL queries do not change. If someone decides that they must add columns to one of your tables in the future (CREATED_BY, LAST_MODIFIED_DATE, etc.), your queries suddenly start returning incorrect data. You waive any reasonable chance to make a reasonable assessment.

    Justin

  • Problems with Server 2008 R2 Kerberos with Mac and CentOS machines? Need to re - join domain

    We are having a problem with our Mac and Linux / CentOS machines constantly having to be re-attached to our AD domain.
    We are able to join machines to the domain successfully, but after a few weeks or if authentication is broken and we again join them to the domain.
    I see Security event logs on our domain controller when kerberos authentication fails.
    On the linux server - I see this message in the logs
    -binding failed: server not found in the kerberos database.

    I'm guessing this has to do with Server 2008 R2 and incompatible mac / linux versions.

    Any ideas?

    Hello

    I suggest you try to post the question in the forums and check them off below if it helps:

    http://social.technet.Microsoft.com/forums/en-us/windowsserver2008r2general/threads

    It will be useful.

  • Problem with a Cartesian join

    Hello

    I'm on Oracle 10.2.0.4.

    I have a problem with a simple Cartesian join. Is it possible to have a Cartesian join? whether a table has data and another table has no data, I take a Cartesian join (no join condition), I want to see all records from a table and values NULL for columns from the other table.

    For example

    SQL > create table T1 (id number, emp_name varchar2 (30));

    Table created.

    SQL > create table T2 (sp_name varchar2 (10), sp_desc varchar2 (10));

    Table created.

    SQL > insert into T1 values (101, 'AAAAA');

    1 line of creation.

    SQL > insert into T1 values (102, 'BBBB');

    1 line of creation.

    SQL > insert into values of T2 ('TBT', 'Important');

    1 line of creation.

    SQL > insert into values of T2 ("XYZ", "Not good");

    1 line of creation.

    SQL > commit;

    Validation complete.

    SQL > select t1.id, t1.emp_name, t2.sp_desc

    2 from t1, (select sp_name, sp_desc from t2 where sp_name = "OTC") t2;

    ID EMP_NAME SP_DESC

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

    AAAAA 101 Important

    Important 102 BENAMER

    SQL > select t1.id, t1.emp_name, t2.sp_desc

    2 from t1, (select sp_name, sp_desc from t2 where sp_name = "ZZZ") t2;

    no selected line

    SQL >

    In the second query (where no line is selected), I want to see all the records in the table T1 and NULL values in columns from the alias of table T2.

    How can I do?

    Thanks in advance

    The ANSI syntax can help here.

    Select t1.id, t1.emp_name, t2.sp_desc

    from t1

    outer join t2 left t2.sp_name = "OTC";

    ID EMP_NAME SP_DESC

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

    AAAAA 101 Important

    Important 102 BENAMER

    Select t1.id, t1.emp_name, t2.sp_desc

    from t1

    outer join t2 left t2.sp_name = "ZZZ";

    ID EMP_NAME SP_DESC

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

    AAAAA 101

    BENAMER 102

    Post edited by: Brian Bontrager
    removed the unnecessary condition

  • Problem with XMLTABLE and LEFT OUTER JOIN

    Hi all.

    I have a problem with XMLTABLE and LEFT OUTER JOIN, in 11g it returns the correct result, but in 10g it doesn't, it is illustrated as a INNER JOIN.
    SELECT * FROM v$version;
    
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
    PL/SQL Release 11.2.0.1.0 - Production
    "CORE     11.2.0.1.0     Production"
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    
    --test for 11g
    
    CREATE TABLE XML_TEST(
         ID NUMBER(2,0),
         XML XMLTYPE
    );
    
    INSERT INTO XML_TEST
    VALUES
    (
         1,
         XMLTYPE
         ('
              <msg>
                   <data>
                        <fields>
                             <id>g1</id>
                             <dat>data1</dat>
                        </fields>
                   </data>
              </msg>
         ')
    );
    
    INSERT INTO XML_TEST
    VALUES
    (
         2,
         XMLTYPE
         ('
              <msg>
                   <data>
                        <fields>
                             <id>g2</id>
                             <dat>data2</dat>
                        </fields>
                   </data>
              </msg>
         ')
    );
    
    INSERT INTO XML_TEST
    VALUES
    (
         3,
         XMLTYPE
         ('
              <msg>
                   <data>
                        <fields>
                             <id>g3</id>
                             <dat>data3</dat>
                        </fields>
                        <fields>
                             <id>g4</id>
                             <dat>data4</dat>
                        </fields>
                        <fields>
                             <dat>data5</dat>
                        </fields>
                   </data>
              </msg>
         ')
    );
    
    SELECT
         t.id,
         x.dat,
         y.seqno,
         y.id_real
    FROM
         xml_test t,
         XMLTABLE
         (
              '/msg/data/fields'
              passing t.xml
              columns
                   dat VARCHAR2(10) path 'dat',
                   id XMLTYPE path 'id'
         )x LEFT OUTER JOIN
         XMLTABLE
         (
              'id'
              passing x.id
              columns
                   seqno FOR ORDINALITY,
                   id_real VARCHAR2(30) PATH '.'
         )y ON 1=1
    ;
    
    ID     DAT     SEQNO     ID_REAL
    --     -----     -----     -------
    1     data1     1     g1
    2     data2     1     g2
    3     data3     1     g3
    3     data4     1     g4
    3     data5          
    This is all nice, now the problem:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for HPUX: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    
    --exactly the same environment as 11g (tables and rows)
    SELECT
         t.id,
         x.dat,
         y.seqno,
         y.id_real
    FROM
         xml_test t,
         XMLTABLE
         (
              '/msg/data/fields'
              passing t.xml
              columns
                   dat VARCHAR2(10) path 'dat',
                   id XMLTYPE path 'id'
         )x LEFT OUTER JOIN
         XMLTABLE
         (
              'id'
              passing x.id
              columns
                   seqno FOR ORDINALITY,
                   id_real VARCHAR2(30) PATH '.'
         )y ON 1=1
    ;
    
    ID     DAT     SEQNO     ID_REAL
    --     -----     -----     -------
    1     data1     1     g1
    2     data2     1     g2
    3     data3     1     g3
    3     data4     1     g4
    As you can see in 10g that I don't have the last row, it seems that Oracle 10 g does not recognize the LEFT OUTER JOIN.

    Is this a bug?, Metalink says that sometimes we can have an ORA-0600, but in this case there is no error results returned, just incorrect.

    Help, please.

    Kind regards.

    What about try the original Oracle method for outer joins? Using (+) without the extra space

    XMLTABLE(...COLUMNS ... id XMLTYPE PATH ... ) x,
    XMLTABLE(... PASSING x.id ...) (+) y
    
  • A curious "anomaly" with the NATURAL JOIN compared to join HER for HELP

    The following query returns I expect since the HR [106 lines] schema:

    Select last_name, department_name of departments who JOIN employees USE (department_id);

    However, when I do the full natural join with this, I get only 32 ranks.

    Select last_name, department_name of the departments of NATURAL JOIN employees;

    The complete NATURAL JOIN not use department_id join on? But if not what else would it use? I just stumbled on this and am especially curious that I do not use the syntax of natural join in production but prefer instead the JOIN now.

    Thank you.

    The NATURAL keyword indicates that a natural join is underway. A natural join is based on all of the columns in the two tables with the same name. He selects the rows of two tables that have equal values in the corresponding columns.

  • problem with join

    Hello Experts,

    I'm having a little problem with joining me. its not taking the values that should be
    declare
    l_start_file_name varchar2(50) := 'liab_report_summary_p008_c';
    l_end_file_name varchar2(50)   := '_english.rep';
    l_file_name varchar2(100);
    l_sql varchar(32767);
    refresh_cdc varchar2(10) := '00540';
    
    Begin
    
    l_file_name := l_start_file_name||REFRESH_CDC||l_end_file_name;
    
     l_sql := 'create or replace directory GTECHFILES as ''/ftpfiles/gtechfiles/acct_'||REFRESH_CDC||'''';
      execute immediate l_sql;
    
    
    l_sql :=  ' CREATE TABLE LOTO_LIABILITY_REPORT_temp             '||
              ' (                                                   '||
              '  draw_number       varchar2(50),                    '||
              '  total_winners_amt number,                          '||
              '  paid_today_amt    number,                          '||
              '  total_paid_amt    number,                          '||
              '  expired_today_amt number,                          '||
              '  total_expired_amt number,                          '||
              '  frac_round_amt    number,                          '||
              '  outstanding_amt   number                           '||
              ' )                                                   '||
              ' ORGANIZATION EXTERNAL                               '||
              '   (  TYPE ORACLE_LOADER                             '||
              '     DEFAULT DIRECTORY GTECHFILES                    '||
              '     ACCESS PARAMETERS                               '||
              '       (                                             '||
              '      RECORDS DELIMITED BY NEWLINE                   '||
              '    BADFILE TEMPDIR:''loto_liability_report.bad''    '||
              '    DISCARDFILE TEMPDIR:''loto_liability_report.dis'''||
              '    LOGFILE TEMPDIR:''loto_liability_report.log''    '||
              '      SKIP 8                                         '||    
              '      FIELDS LDRTRIM                                 '||
              '      MISSING FIELD VALUES ARE NULL                  '||
              '       (                                             '||
              '  draw_number       (1:11)  char(11),                '||
              '  total_winners_amt (14:24) char(11),                '||
              '  paid_today_amt    (26:36) char(11),                '||
              '  total_paid_amt    (38:48) char(11),                '||
              '  expired_today_amt (50:60) char(11),                '||
              '  total_expired_amt (62:72) char(11),                '||
              '  frac_round_amt    (74:80) char(7),                 '||
              '  outstanding_amt   (82:92) char(11)                 '||
              '       )                                             '||
              '   )                                                 '||
              '     LOCATION ('''||l_file_name||''')                '||
              ' )                                                   '||
              ' REJECT LIMIT UNLIMITED                              '||
              ' NOPARALLEL                                          '||
              ' NOMONITORING                                        ';
    
             execute immediate l_sql; 
    
    
     l_sql := ' insert into LOTO_LIABILITY_REPORT '||
     ' ( draw_number             , '||
     '   total_winners_amt       , '||
     '   paid_today_amt          , '||
     '   total_paid_amt          , '||
     '   expired_today_amt       , '||
     '   total_expired_amt       , '||
     '   frac_round_amt          , '||
     '   outstanding_amt         , '||
     '   source                  , '||
     '   inserted_DATE           , '||
     '   CDC                     , '||
     '   draw_start_date         , '||
     '   draw_end_date             '||
     ' )                           '||
     '   SELECT                    '||
     '       a.to_number(substr(draw_number,1,instr(draw_number,''/'',1)-1)) , '||
     '       a.total_winners_amt                                             , '||
     '       a.paid_today_amt                                                , '||
     '       a.total_paid_amt                                                , '||
     '       a.expired_today_amt                                             , '||
     '       a.total_expired_amt                                             , '||
     '       a.frac_round_amt                                                , '|| 
     '       a.outstanding_amt                                               , '||  
     '       '''||l_file_name                                           ||''', '||
     '       sysdate                                                         , '||
     '       '''||refresh_cdc                                           ||''', '|| 
     '       b.start_date                                                    , '|| 
     '       b.end_date                                                        '|| 
     '    FROM                                                                 '||
     '       a.LOTO_LIABILITY_REPORT_temp                                     ,'||
     '       b.draws                                                           '||
     '    where                                                                '||
     '       a.draw_number is not null                                         '||
     '    and                                                                  '||
     '       a.total_winners_amt is not null                                   '||
     '    and                                                                  '|| 
     '       a.to_number(substr(draw_number,1,instr(draw_number,''/'',1)-1))= b.num ';
    
     
        dbms_output.put_line(l_sql);
             execute immediate l_sql;           
             
    l_sql :='drop table LOTO_LIABILITY_REPORT_temp';
     
            execute immediate l_sql;
            
    exception
    when others then
    rollback;
    l_sql :='drop table LOTO_LIABILITY_REPORT_temp';
    execute immediate l_sql;
    debug_message('LOTO_LIABILITY_REPORT_temp_UPLOAD/'||REFRESH_CDC,'Unexpected Error '||sqlerrm);
    END LOTO_LIABILITY_REPORT;

    at least

     '    FROM                                                                 '||
     '       LOTO_LIABILITY_REPORT_temp a                                     ,'||
     '       draws b                                                           '||
     '    where                                                                '||
    

    Concerning

    Etbin

  • problem with join of two tables

    Hi, consider the following data
    WITH data AS
    (
      SELECT 123 cid, 'aaa' isi, 'kkk' sud, 'ttt' ri FROM dual UNION ALL 
      SELECT 222 cid, 'bbb' isi, 'gggg' sud, 'hhh' ri FROM dual
    
    ) ,
    data2 AS 
    (
      SELECT 'aaa' isi, 'yyy' sud, 'ooo' ri FROM dual UNION ALL 
      SELECT 'qqq' isi, 'ggg' sud, 'ooo' ri FROM dual UNION ALL 
      SELECT 'uuu' isi, 'ppp' sud, 'ttt' ri FROM dual UNION ALL
       SELECT 'ppp' isi, 'mmm' sud, 'nnn' ri FROM dual 
    
    
    )
    what I want to do, is to join data2 with data by isi, ri or southern id table and produce this output.
     cid  txt
     ===  ====
     123  aaa
     222  ggg
     123  ttt
    It's how I got the above output:
    first try to join isi in database2 with isi in "features" and discover if line matches. If this is the case, display the cid of the data table and the use of the value to match.
    e.g. in the first line of table data2, we can see that isi = aaa matches line an isi data table. If the output displays 123 and value aaa.

    If the rank table two of the Data2 isi = qqq. When join them with the data table, we find not all isi with qqq. in this case, it must join with column of South. We can see that South = ggg data2 games
    the second line in the table of output so will bee 222 and value ggg.

    now, take a look at the third row in Database2. ISI = uuu is not found in the data table when join them. then try to join to the South. South = ttt is also not found in any row of the data table. so join with ri. We
    can see that ri = ttt data2 matches with ri = ttt in the table of data online 1. Therefore, we display the value.

    can someone help me write a sql query that joins these two tables by isi. If no line found then join South, if no found rows then join in ri? Thank you

    Hello

    elmasduro wrote:
    Hi Frank, data2.sud = 'Gay' (4 characters) assume to be South = "ggg". by mistake, I type extra g.

    I have another question about the request. What happens if I want to bring rows of data.2 even if they do not exist in the data table for the entire column was join?
    for example, I want to show this:

    cid  txt
    ===  ====
    123  aaa
    222  ggg
    123  ttt
    ppp  --row from data2
    

    the columns of the last row of data.2 does not match any column in the data table. in this case, I want to make but cid will be null. I tried outer join on your request, but it looks like I can not clause or with outer join

    SELECT     d1.cid
    ,     CASE
              WHEN  d1.isi = d2.isi  THEN  d2.isi
              WHEN  d1.sud = d2.sud  THEN  d2.sud
                                            ELSE  d2.ri
         END     AS txt
    FROM data     d1
    JOIN     data2     d2  ON     d1.is(+)i     = d2.isi
                  OR     d1.sud(+)     = d2.sud
                  OR     d1.ri(+)     = d2.ri
    ;
    

    How can I re - write the query for this case? Thanks again for your help

    Here is the outer join syntax:

    SELECT     d1.cid
    ,     CASE
              WHEN  d1.isi = d2.isi  THEN  d2.isi
              WHEN  d1.sud = d2.sud  THEN  d2.sud
              WHEN  d1.ri  = d2.ri   THEN  d2.ri
                                            ELSE  d2.isi
         END     AS txt
    FROM          data2     d2
    LEFT OUTER JOIN     data     d1  ON     d1.isi     = d2.isi
                           OR     d1.sud     = d2.sud
                           OR     d1.ri     = d2.ri
    ;
    

    The + sign for outer joins only works with the old join syntax, non-ANSI. Do not try to mix the two different ways to do joins.

    Not to mention that the outer join, don't forget to change the CASE expression, to include the new possibility of any columns 3 join matching.

  • Problems with joins in the repository

    Hello

    I have a requirement to display a report which shows that the activity of the employee account within a specified date range.

    My tables is:

    Emp_Dim (emp_key, emp_name, etc.)
    Date_Dim (date_key, date_value, etc.)
    Fact_Activities (emp_key, date_key, County of activity)

    The part that gives me, is that the customer wants to see all employees without worrying if they have an entry in the fact_activities table. I tried messing around with outer joins business layer between these tables, but I can't seem to get the results the customer wants.

    When a user enters a date range in a command prompt, the report should show all employees in the organization. The counties of activity should reflect the date range. So if the employee had a count of 5 between April 6 and April 12, but a number 0 between April 13 and April 15, this should be reflected when the user changes the date range in the report.

    Currently, if an employee count 0 activity for a certain date range, they are not just displayed in the report for this range.

    Any ideas?

    Thank you

    I'm glad that solve you it but I am very surprised because I well I posted the filing solution that is a better solution.

    Check here:
    http://gerardnico.com/wiki/dat/OBIEE/bi_server/design/obiee_densification_design_preservation_dimension

    You must use a small obiee logic sql in the filter to say that only users who have an activity for the desired period.

  • Incorrect results of natural join producing. I was wondering why.

    If anyones got a second, I have a small question about this bit of code. It seems that he should produce good results, but does not work.

    "Write a query to identify the highest salary paid in each country. It will take by using a subquery in the FROM clause. »

    It is the obvious answer:

    "select max (salary), country_id of."
    (select department_id, location_id, salary, country_id of)
    employees join natural services locations of natural join)
    Country_id group; »

    Here are the results:

    17000 WE
    CA 6000
    10000 UK


    I wrote this instead:

    Select max (salary), country_id of
    (select d.department_id, l.location_id, c.country_id, e.salary, c.country_name
    e employees
    Join departments d on (e.department_id = d.department_id)
    Join places l on (d.location_id = l.location_id)
    Join country c on (l.country_id = c.country_id)
    )
    Country_id group;

    Which produces:

    24000 WE
    13000 CA
    10000 OF
    14000 UK

    (Of course it also works with 'join using (column_name)')

    My answers look correct when passing through the countries/places/employee tables manually - look of the wrong example.

    I guess its something to do with the natural join? Maybe his join tables somewhere on several columns? I would like to understand exactly why the first example is if bad - because I sure hope will teach me more about why avoid natural joins.

    Also, are there ways to improve outcomes, for example if I was watching an average salary? I drew the tables to try to find ways that the records may be left out, and where the outer joins might be useful. The only thing I could think of would be that a Department/location not assigned a place/country respectively. But I'm not an outer join would allow it at all except if I created some NVL2 code to assign new place/country IDs based on other areas - I think that's a bit much for this example.

    Thank you very much

    Nick

    Published by: nick woodward on April 22, 2013 11:10

    Published by: nick woodward on April 22, 2013 11:12

    Hi, Nick.

    Nick woodward wrote:
    If anyones got a second, I have a small question about this bit of code. It seems that he should produce good results, but does not work.

    "Write a query to identify the highest salary paid in each country. It will take by using a subquery in the FROM clause. »

    In fact, a subquery in the FROM clause is not yet useful, at least not for me, and it is certainly not necessary.

    It is the obvious answer:

    "select max (salary), country_id of."
    (select department_id, location_id, salary, country_id of)
    employees join natural services locations of natural join)
    Country_id group; »

    Here are the results:

    17000 WE
    CA 6000
    10000 UK

    I wrote this instead:

    Select max (salary), country_id of
    (select d.department_id, l.location_id, c.country_id, e.salary, c.country_name
    e employees
    Join departments d on (e.department_id = d.department_id)
    Join places l on (d.location_id = l.location_id)
    Join country c on (l.country_id = c.country_id)
    )
    Country_id group;

    Which produces:

    24000 WE
    13000 CA
    10000 OF
    14000 UK

    (Of course it also works with 'join using (column_name)')

    AID is not as bad as NATURAL JOIN, but it's pretty bad. Forget the JOIN... Help me, just like you should forget NATURAL JOIN.

    My answers look correct when passing through the countries/places/employee tables manually - look of the wrong example.

    I guess its something to do with the natural join? Maybe his join tables somewhere on several columns?

    Exactly; There is a column manager_id employees and also in the departments.
    If the obligation is to get the highest salary of all employees in a country, then NATURAL JOIN does not meet the requirements.

    I would like to understand exactly why the first example is if bad - because I sure hope will teach me more about why avoid natural joins.

    Do you need any other reasons?

    Also, are there ways to improve the results

    As I said earlier, you don't need a subquery for this.
    In addition, you must the country table if you want to display the country_name, or if you need to reach the region table. In this problem, all you need is the country_id and you can get that from the communities table, then the following text also gets good results:

    SELECT    MAX (e.salary)     AS max_salary
    ,        l.country_id
    FROM      employees    e
    JOIN      departments  d  ON  d.department_id = e.department_id
    JOIN      locations    l  ON  l.location_id   = d.location_id
    GROUP BY  l.country_id
    ORDER BY  l.country_id
    ;
    

    Whenever a request is for 2 or more tables, it is recommended to describe each column with a table name or alias.

    say if I watched an average salary?

    You can use AVG much lke you use MAX. You can do both in the same query, if you wish; See the example below.

    I drew the tables to try to find ways that the records may be left out, and where the outer joins might be useful. The only thing I could think of would be that a Department/location not assigned a place/country respectively.

    Right; outer joins are useful when you want to get data from the table or not a matching rows in the table b. Departaments that have not been allocated a location is a situation that calls for an outer join. Another example is the country who have no departments in them. (There is a real possibility. You can set up a table with all countries in the world already in it, not because you'll need all of them, but because you might need one of them.)

    But I'm not an outer join would allow it at all except if I created some NVL2 code to assign new place/country IDs based on other areas - I think that's a bit much for this example.

    Not necessarily. Sometimes all you need is the NULL that you get automatically for all columns of table b when you say

    FROM             a
    LEFT OUTER JOIN  b  ...
    

    and one has no corresponding row in the b

    For example, if you want to include all countries in the table of localities, with the salary maximum and average of those that have employees, you can get these results:

    MAX_SALARY AVG_SALARY CO
    ---------- ---------- --
                          AU
                          BR
         13000       9500 CA
                          CH
                          CN
         10000      10000 DE
                          IN
                          IT
                          JP
                          MX
                          NL
                          SG
         14000 8885.71429 UK
         24000 5064.70588 US
    

    without using NVL, or NVL2 or something like that. In fact, only the functions you need are AVG and MAX.
    Try it.

    Published by: Frank Kulash on April 22, 2013 16:35

  • problems with, phone, 6, Bluetooth kit, Nissan, after update, for, Rios, 1.0.2

    After the update to ios 10.0.2 - trying to use bluetooth to call my vehicle, it says: "this article is not in your phone book." How can I solve this problem?

    Greetings, joybelino1!

    Thank you for joining the communities Support from Apple! I can't wait to see that you are having problems with your Bluetooth in your car! The good news is that Apple has a great article that will help you with measures to try to resolve the problem. Read this article to gethelp to connect your iPhone, iPad, or iPod touch with your car radio. Even though he talks about problems with the connection, it also has the steps for other questions you may have once connected.

    If you use Bluetooth

    1. Consult the user manual of your car stereo to get the procedure to a Bluetooth device.
    2. On your iOS device, drag up to open Control Center, then press ontwice to turn on Bluetooth and turn it back on.
    3. Restart your iOS device.
    4. On your iOS device, Cancel the twinning of your car radio. On the screen of your car désapparier your iOS device and any other device. Restart your car and your iOS device, then pair and connect again.
    5. Update your iOS device.
    6. Install the updates to the firmware of your car radio.
    7. If you still not connect, contact Apple technical support.

    Have a great day!

Maybe you are looking for

  • Apple ID disabled-&gt; not a Apple ID

    Hello world! I changed my ID apple some time ago, but after buying a new iPhone it connected to my old. Now, I can not turn off in iCloud parameters by any medium that when I try to enter the old password it says my ID has been disabled, but when I t

  • y510p issue replacement of hard drives (no SLi)

    I would like to replace the hard drives on my y510p and bought a 1 TB HGST 7200 RPM for the essential and crucial m500 240 GB 2.5 SSD to replace the 24 GB SSD that is already there. Now around reading it seems that I won't be able to do it easily? I

  • Export release build error

    I have my loan application. It does not correctly work in the emulator. Now, I want to create the version that I can submit to the App World. I use Flash Builder Burrito. That's what I do. 1) click Project > export Release Build from the main menu. (

  • Unable to connect old keyboard to new computer using a usb adapter

    original title: I just got a new pc with windows 7, I can't connect my old keyboard to it, I have an adapter from Tower to the usb port on the keyboard, microsoft natural multimedia keyboard 1. 0 to help would be appreciated ty___ I just got a new pc

  • Windows 8 operating system is compatible with a new Panasonic Toughbook 19?

    Windows 8 operating system is compatible with a new Panasonic Toughbook 19?  The sales representative is indicating that they do not have the COA available Win8.  Does this mean that I can buy the Toughbook 19 with the installed operating system Wind