Left outer join without data in a table

I have two tables defined (see below). Emp table has given, and there is still no data in the table of Emp_Type! Now it is empty.

I want to write a query that returns the data from the tables, even if there is no data in the Emp_type table. I use a left outer join but it return nothing. Can anyone help?
create table EMP
(
  EMPID   NUMBER(10,2),
  EMPNAME VARCHAR2(100)
);

INSERT INTO emp (empid,empname) values (1, 'Mark');
INSERT INTO emp (empid,empname) values (2, 'Jason');
INSERT INTO emp (empid,empname) values (3, 'Kevin');
INSERT INTO emp (empid,empname) values (4, 'Drew');
INSERT INTO emp (empid,empname) values (5, 'Jessica');
INSERT INTO emp (empid,empname) values (6, 'Pena');
INSERT INTO emp (empid,empname) values (7, 'Roxanne');

create table EMP_Type
(
  ID          NUMBER(10,2),
  EMPID       NUMBER(10,2),
  TYPE_ID     NUMBER(10,2),
  END_DATE    DATE
);

No data

select *
  from emp e
  left outer join emp_Type t
    on e.empid = t.empid
 WHERE t.type_id = 1
   and t.end_date is null;

WHERE t.type_id = 1

will be ever true when table has no rows (or t.type_id is NULL?)

Tags: Database

Similar Questions

  • Oracle: Use LEFT OUTER JOIN, but convert the data to an external list

    Hello, all,.

    I know it can be done; I just don't remember how I got it done, oh there are so many years.

    Assumes that the tables exist for groups and individuals.  People can belong to several groups.

    SELECT g.groupName, p.lastName || ', ' || p.firstName as fullName
    FROM groups g LEFT OUTER JOIN groupPersonAssociation gpa ON gpa.groupID = g.groupID
                  LEFT OUTER JOIN person p ON p.personID = gpa.personID
    ORDER BY g.groupName, fullName
    

    This gives us:

    Group One          Alpha, Daniel
    Group One          Bravo, Charles
    Group One          Charlie, Chuck
    Group Two          Beta, Alpha
    Group Two          Delta, Bonnie
    Group Three        Echo, Bunny
    Group Three        Golf, Samuel
    Group Three        November, Stan
    

    How word the SQL to get the data as:

    Group One          Alpha, Daniel | Bravo, Charles | Charlie, Chuck
    Group Two          Beta, Alpha | Delta, Bonnie
    Group Three        Echo, Bunny | Golf, Samuel | November, Stan
    

    V/r,

    ^_^

    I finally thought to it.  I was using incorrect keywords on Google.

    SELECT g.groupName, LISTAGG(p.lastName || ', ' || p.firstName,' | ') WITHIN GROUP (ORDER BY g.groupName) "fullName"
    FROM groups g LEFT OUTER JOIN groupPersonAssociation gpa ON ggpa.groupID = g.groupID
                  LEFT OUTER JOIN person p ON p.personID = gpa.personID
    GROUP BY g.groupName
    ORDER BY g.groupName, fullName  
    

    Just in case someone else is going through this same desire.

    HTH,

    ^_^

  • left outer join and the where clause for the table to the right

    I want to join two tables a and b, where a is a must and b is a result set in option. When I use a left outer join to a to b, I want to achieve:

    1. Select a single column, two columns of b (not the join columns)
    2 - even if theres no friendly on the join column does not return data from one.
    3. If there is a match applies when the criteria on column b (table in option)

    so, how can I avoid no_data_found in this case? When I apply where criteria for b, so it does not return the data from one, which is a must.

    Sounds like a regular outer join to me...

    select a.col1, b.col2, b.col2
    from   tableA a
           left outer join tableB b
           on (a.id = b.id and b.colX = 'X')
    
  • The left join - no data in the table don't want to display the data from the table one

    Hello

    I'm writing a query. Here is my scenario, I have two tables EMP and EMP_TYPE. I want to join both the table and even if there is no matching record in the table for EMP EMP_TYPE, I want to return the record to the EMP table and empty values of EMP_TYPE.
    create table EMP
    (
      EMPID   NUMBER(10,2),
      EMPNAME VARCHAR2(100)
    );

    INSERT INTO emp (empid,empname) values (1, 'abc');
    INSERT INTO emp (empid,empname) values (2, 'xyz');
    INSERT INTO emp (empid,empname) values (3, 'UNJ');

    create table EMP_Type
    (
      ID          NUMBER(10,2),
      EMPID       NUMBER(10,2),
      TYPE_ID     NUMBER(10,2),
      END_DATE    DATE
    );

    INSERT INTO EMP_Type(id, empid, TYPE_ID, END_DATE) values (100, 2, 1, SYSDATE);

    select *
      from emp e
      left outer join emp_Type t
        on e.empid = t.empid
    and t.type_id = 1

    returns.

            EMPID     EMPNAME     ID             EMPID     TYPE_ID     END_DATE
         2.00             xyz            100.00     2.00             1.00             10/9/2012 5:18:59 PM
         3.00             UNJ                    
         1.00             abc                    


    select *
      from emp e
      left outer join emp_Type t
        on e.empid = t.empid
    and t.type_id = 1
    and t.empid = 3

    returns

            EMPID     EMPNAME     ID             EMPID     TYPE_ID     END_DATE
         2.00             xyz                 
         3.00             UNJ                    
         1.00             abc          

    but instead I want

            EMPID     EMPNAME     ID             EMPID     TYPE_ID     END_DATE
         3.00             UNJ                    

    Your conditions of conflict
    >
    and t.type_id = 1
    and t.empid = 3
    >
    There is a record with t.empid 3

    select *
      from empx e, empx_Type t
    where e.empid = 3
      and e.empid = t.empid (+)
    
    EMPID,EMPNAME,ID,EMPID_1,TYPE_ID,END_DATE
    3,UNJ,,,,
    
  • Show all dates between date range (time Dimension is left outer join)

    All,

    I did some research on this issue, but in all positions on date variables, date prompts and date filtering I have not seen one exactly to my question (perhaps that they are and I don't have my head around it properly yet).

    My requirement of report is to allow a user to select a start date and an end of day. The report is expected to show the activity of these two days - AND display 0/null on days where there is no activity. This second part is where I am getting hung up.

    The paintings in question are:
    Timedim
    EventFact
    CustomerDim

    My MDB is configured as follows:
    Left outer join of Timedim EventFact
    Inner join CustomerDim EventFact

    If I run a report by selecting the DAYS of Timedim and an EventFact measure1 with range day 01/01/2010-31/12/2010... A record for each day and it looks perfect because of the left outer join between Timedim and CustomerDim.

    But... If I add a CustomerDim field, Select TimeDim.DAY, CustomerDim.CUSTNAME, EventFact.MEASURE1, OBIEE returns only records for the days that have record EventFact.

    This is due to the fact that the Timedim is always external joined in EventFact, but adding in fact CustomerDim OBIEE set up an inner join between tables that will only return data where there are data EventFact.

    There is a way around it in this simple case, and that is to define the relationship between CustomerDim and EventFact as an outer join as well. This will give the desired effect (but an outer join between the two tables is not the real relationship) and I have add an extra dimension and add additional sources of logic to a single dimension in MDB it becomes complicated and messy.

    Also, ive ruined with the definition of the conduct in the relationship table, etc... but he gave not the desired effect.

    Has anyone ever met the need for force display all dates within a range specified with a fact table that does not have an entry for each date?

    Thanks in advance.

    K

    Published by: user_K on April 27, 2010 11:32

    Hi there, the easiest way is to the LTS himself. Double-click your LTS, go to the tab with the column mappings. Make sure you have checked "show no mapped" column.

    You should see your new dummy column in the list, in the central part of mapping (IE not the right) just enter 0, or launch the expression editor and enter 0 in there.
    simple!

  • Why left outer join with a table gives me more lines?

    Hi gurus,

    I can see "view_a" and a table 'table_a '.

    view_a a county of 100 lines. Now, when I left outer join that discovers with a 'table_a', I expect all 100 lines.

    However, I'm more than 100 lines. Is it still possible?

    Also even to analyze these situations, how can I move forward?

    Because it is very high volumn of sight and takes longer to run.

    Select count (*) view_a, view_b

    where view_a.col1 = view_b.col1 (+)

    and view_a.col2 = view_b.col2 (+);

    Thank you

    I can see "view_a" and a table 'table_a '.

    view_a a county of 100 lines. Now, when I left outer join that discovers with a 'table_a', I expect all 100 lines.

    However, I'm more than 100 lines. Is it still possible?

    Also even to analyze these situations, how can I move forward?

    Because it is very high volumn of sight and takes longer to run.

    Select count (*) view_a, view_b

    where view_a.col1 = view_b.col1 (+)

    and view_a.col2 = view_b.col2 (+);

    Which is not necessarily related to the use of an outer join.

    Just join of two tables in general will give you more rows of one table has.

    Scott DEPT table contains ONE row for deptno = 10

    The EMP table has THREE rows of deptno = 10

    The number of rows you plan if you join two tables using an equi-join?

    Three - what is MORE lines the DEPT table has for deptno = 10

    Select * from Department where deptno = 10

    DEPTNO, DNAME, LOC
    10, ACCOUNTING, NEW YORK

    Select * from emp where deptno = 10

    MGR, EMPLOYMENT ENAME, EMPNO, HIREDATE, SAL, COMM, DEPTNO
    7782, CLARK, MANAGER, 7839, 6/9/1981,2450, 10
    7839, KING, PRESIDENT, 17 NOVEMBER 00, 10
    7934, MILLER, CLERK, 7782, 23 JANUARY 00: 10

    Select dept.*, emp.*
    Department, emp
    where dept.deptno = 10
    and dept.deptno = emp.deptno

    DEPTNO, DNAME, LOC, EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO_1
    10, ACCOUNTING, NEW YORK, 7782, CLARK, MANAGER, 7839, 6/9/1981,2450, 10
    10, ACCOUNTING, NEW YORK, 7839, KING, PRESIDENT, 17 NOVEMBER 00, 10
    10, ACCOUNTING, NEW YORK, 7934, MILLER, CLERK, 7782, 23 JANUARY 00: 10

    So if these are the lines ONLY in the table EMP and DEPT the query would give you THREE lines despite the DEPT table only ONE line.

    No do you expect? You get ALL the child rows that belong to the parent company. Otherwise, how could it possibly work?

    The OUTER join includes lines where the parent row exists but there is NO child line as others have shown.

    Outer joins

    Outer join extends the result of a simple join. Outer join returns all rows that satisfy the join condition and also returns some or all rows in a table for which no line of the other meet the join condition.

    Get more lines to exist in one of the paintings is a basic necessity. It usually has NOTHING to with the question of whether you have an outside to join or not.

    See the section on the JOINTS in the Oracle documentation

    http://docs.Oracle.com/CD/B28359_01/server.111/b28286/queries006.htm

  • 3 left outer join tables

    Hi all

    I have 3 tables A, B, C

    Create table a (varchar2 (100)) of the currency;

    insert into a values (GBp);

    insert into a values (GBP);

    insert into a values (GBX);

    Create table B (varchar2 (100) currency, number);

    insert into B values (GBP, 61.1);

    Create a table (minor_currency varchar2 (100), major_currency varchar2 (100));

    insert into values of C (GBp, GBP);

    insert into values of C (GBX, GBP);

    I need to get the rate table B by linking the A with the currency as a condition of joining.  (left outer join)

    For currencies which are not in table B, table should be attached with C minor currency-based

    and get the major_currency and join with table B

    Ex:

    something like this:

    Select B.rate from A, B, C

    WHERE (A.currency = B.currency or (A.currency = C.minor_currency and B.currency = C.major_currency)

    O/P: for GBp and GBX currency, I need to get the rate as 61.1 in table B, but B currency is GBP. So I need to get the major_currecny for GBp, GBX table C and join with the table B

    Thank you

    Sasi

    Hi all

    Thanks for your time. Its done

  • Left outer join on a derived Table

    Hello:

    I get an "ORA-00905: lack of keyword" error when I run the following query - don't know what I'm doing wrong here - any suggesstions will be very useful

    Thank you


    SELECT count (*)
    FROM aradmin.ast_asset a left outer join
    (
    Select audit_hist_asset, Deploy_Time
    of (a.asset_entry_id select audit_hist_asset, to_localtime (a.change_date, 'IS') Deploy_Time,)
    ROW_NUMBER() over (partition by order of a.change_date a.asset_entry_id) rn
    of aradmin.ast_bms_audit_history one
    where a.change_type = 'status '.
    and a.change_to = 'Deployed')
    where rn = 1
    ) AS b
    On a.asset_entry_id = b.audit_hist_asset

    Hello

    Good job on putting in shape! It is easier to read now.

    The error message included a line number? You use a.asset_entry_id in two different places, to refer to two different tables. Who is confused, but niot necessarily a mistake. You should use different tables for aradmin.ast_asset aliases in the main query and aradmin.ast_bms_audit_history in the internal subquery.

    Two of these tables have a column called asset_entry_id?

  • Left Outer Join DR.

    Hi Experts,

    I have a requirement that says - see the chart for the past 10 days, regardless the presence data table in fact.
    Lets consider an example - Time_dim product, are my dimension tables, Purchase_Order is my fact table.

    I did it for external Purchase_Order in left RPD with TIME_DIM and inner join with the PRODUCT table.  and execution of query of exit-
    Select T.Date, P.item, count (distinct PO.order_no)
    TIME_DIM t, PRODUCT P, PURCHASE_ORDER PO
    where T.date_key = PO.date_key
    and P.item = in. agenda
    and P.item = 'laptop ';

    The query generated by OBIEE left outer join, but when the condition P.item = "Notebook" included in the query, and if there are no orders for this product in one of the date, that date will not come in the result set.

    the query to be generated by the OBIEE is-


    Select T.Date, PO.item, count (distinct PO.order_no)
    TIME_DIM t,.
    (SELECT P.ITEM, IN. ORDER_NO
    PRODUCT P, PO PURCHASE_ORDER
    WHERE P.item = in. agenda
    and P.item = 'Laptop') IN.
    WHERE T.date_key = PO.date_key (+);

    How to design the RPD to achieve this. All pray to advise on this. Thanks in advance.

    Thank you
    Chantal

    Hello

    You are on 11.1.1.7?

    I would say that your condition can be made without using external and maintenance of product and the standard between the FACT dimension, time inner join join.

    If you enable your property analysis OBIEE "Include Null values" will automatically return all the elements of time and product matching your filter (so you'll need to add a filter on 'Date' to limit it to the last 10 days or you will have a unique day of your time dimension).

    If you filter then on "Laptop", even if there is not a single value in order for "Laptop" in the last 10 days, he will be there on the screen.

    Easy, clean and you keep your inner join between the facts and Dimensions.

    Take a look at this example, I just did on SampleApp 406:

    Selection of 12 months (year 2010) and a customer (id = 89) and income. The model has only an inner join. I activate the option "Include Null values" and here is the result.

    A line with cells only empty because there is not a single revenue for customer 89 in 2010. This is exactly your condition.

    Honestly, do not touch your model using the outer join, you will have more side effects than benefits. Every single scan will do the outer join and you'll have a lot of data 'empty' return of the DB (more large data set containing just the null values) and probably you need the outer join in 15 to 25% of your analysis.

    Keep things simple, it will be faster and easier to maintain.

  • 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
    
  • Modeling of the left outer join

    Hello world

    I'm tender hand to you guys for a modeling help

    I have a FACT, the customers, the Dim_Date and CUST_ADDRESS of tables to model

    Fact and the client are joined through CUST_ID

    FACT and DATE are joined through DATE_ID

    CUST_ADDRESS must be attached to the top of the model through CUST_ID, DATE_ID and this join must be Left outer because sometimes the address does not exist or is not current, which means DATE_ID could be different between Dim_Date and CUST_ADDRESS

    If it were to join internal, model would have been easy, because of the outside left that I am unable to model, it's pretty good.

    Application under
    Select D.DATE, C.CUST_NAME, CA. ADDRESS, F.AMOUNT
    Of
    F FACT
    JOIN THE
    CUSTOMER C
    ON C.CUST_ID = F.CUST_ID
    JOIN THE
    DIM_DATE D
    ON F.DATE_ID = D.DATE_ID
    LEFT OUTER JOIN
    CUST_ADDRESS CA
    ON C.CUST_ID = CA. CUST_ID AND C.DATE_ID = D.DATE_ID

    Thanks in advance

    When I add the CUSTOMER and in FACT LTS CUST_ADDRESS

    Stop it!

    Don't add CUSTOMER and CUST_ADDRESS in the FACT of LTS. Why would add you to the LTS DO?

    You design a management model: CUSTOMER is a dimension and it has its own logical table this logic table join with a logical join in the activity diagram. Ditto for CUST_ADDRESS.

    So the change, I missed earlier is CUST_ADDRESS contains no Cust_ID (ACTUALLY existing), but contains a Cust_NO, and the table to translate Cust_NO in Cust_ID is CUSTOMER?

    No problem...

    Let's start with a new alias of CUSTOMER (to keep more simple to understand at the moment), call as you want, but this new alias will be the link between the FACT and CUST_ADDRESS.

    In LTS of the dimension 'Address', you have CUST_ADDRESS initially, add an inner join on the new alias that you created in the LTS of the CUSTOMER. So now your 'Address' logical dimension contains the Cust_NO and Cust_ID and this will make the join to FACT.

    Between CUST_ADDRESS and the CLIENT, you can keep an inner join, because the target is not not for get the address of the customer, but is having the Cust_ID in the address line.

    Give it a try at that.

    But do not add these tables in the LTS, they are logical dimensions.

  • Help for a LEFT OUTER JOIN query

    Hello, all,.

    I'm having some trouble setting up an Oracle 11 g Server SQL query, and I could use some help.

    Let's say tableA is blogs; tableC is comments for blog entries; tableB is the associative array:

    tableA
    blogID        blogTitle       blogBody      dateEntered
    1             This is a test  More text...  2016-05-20 11:11:11
    2             More testing    Still more!   2016-05-19 10:10:10
    3             Third charm!!   Blah, blah.   2016-05-18 09:09:09
    

    tableC
    commID        userID          userText      dateEntered
    10            Bravo           I like it!    2016-05-20 11:21:31
    11            Charlie         I don't!      2016-05-20 11:31:51
    12            Alpha           Do it again!  2016-05-19 10:20:30
    13            Bravo           Still more?   2016-05-19 10:30:50
    14            Charlie         So, what?     2016-05-19 10:35:45
    15            Bravo           Blah, what?   2016-05-18 09:10:11
    16            Alpha           Magic number! 2016-05-18 09:11:13
    

    tableB
    blogID        commID
    1             10
    1             11
    1             12
    2             13
    2             14
    3             15
    3             16
    
    
    
    

    I'm trying to get blogID, blogTitle, blogBody and the number of comments for each blog entry.  But, since I'm on to_char() for date and COUNT (commID) for the total number of comments, I am not "a group by expression.

    Here is an example of pseudo-SQL of what I'm trying.

    SELECT a.blogID, a.blogTitle, a.blogBody, to_char(a.dateEntered,'YYYY-MM-DD HH24:MI:SS') as dateEntered, COUNT(c.commID) as total
    FROM tableA a LEFT OUTER JOIN tableB b ON b.blog_ID = a.blog_ID
                  LEFT OUTER JOIN tableC c ON c.commID = b.commID
    WHERE a.blogID = '1'
    GROUP BY blogID, blogTitle, blogBody
    ORDER BY to_date(dateEntered,'MM-DD-YYYY HH24:MI:SS') desc
    

    I'm sure it's something simple, but I just DO NOT see it.  Can you help me?

    V/r,

    ^_^

    Try:

    GROUP BY a.blogID, a.blogTitle, a.blogBody, to_char(a.dateEntered,'YYYY-MM-DD HH24:MI:SS')
    

    See you soon

    Eddie

  • The table can be outer joined to a single other table

    Hello

    Using oracle 11.2.0.3

    Select *.

    from tablea

    tableb

    tablec

    where tablea.col = tableb.col (+)

    and tablea.col = tablec.col (+)

    throws error table can be attached externally to another table.

    What is the best/recommended way to allow more than one outer join table?

    Thank you

    Hello

    Use ANSI join rating:

    SELECT *.

    FROM tablea a

    LEFT OUTER JOIN tableb b ON a.col = b.col

    LEFT OUTER JOIN tablec c ON a.col = c.col

    ;

    Are you sure the query you posted is really what you're running?  The query you posted shoulddn can't cause the error, because the tableb and tablec are each being only outside attached to 1 other table.  You get the error if the + signs were reversed in the two conditions.

    In any case, I suggest to use the ANSI syntax for all joins, especially outer joins.  You will never get the error "Table can be external attached to a single other table", and which is one of the reasons why it is better.

    I hope that answers your question.

    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.

    Explain, using specific examples, how you get these results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • left outer join, after summarizing

    Hello

    I need to create a summary of a lot of tables and join the result set with another table. The other table must do a left outer join. When I do a left outer join, the column which do not appear in the second table appear as a draft. However, I would like to than all results in the table on the right to replicate once for each row in the left table.  Here is a sample data set:

    CREATE THE TRIGHT TABLE

    (

    PKEY CHAR(1),

    AMT NUMERIC (13.2).

    GRP TANK (4),

    FILTER TANK (3)

    )

    INSERT INTO TRIGHT VALUES ('A', 200 ' GRP1 "," XXX ");

    INSERT INTO TRIGHT VALUES ('A', 500, "GRP2', 'XXX'");

    INSERT INTO TRIGHT VALUES ('A', 150 'GRP1', 'YYY');

    INSERT INTO TRIGHT VALUES ('A', 100, "GRP2', 'YYY'");

    CREATE THE TLEFT TABLE

    (

    PKEY CHAR (1)

    )

    INSERT INTO VALUES TLEFT ('A')

    INSERT INTO TLEFT VALUES ('B')

    SELECT A.PKEY, GRP, SUM (AMT) OF TLEFT A LEFT JOIN

    (SELECT PKEY, GRP, SUM (AMT) AS AMT IN TRIGHT)

    WHERE FILTER = "XXX".

    PKEY, GRP) B

    ON A.PKEY = B.PKEY

    A.PKEY GROUP, GRP

    The result I get is:

    A 200 GRP1

    A 500 GRP2

    B (white) (white)

    Instead, I would like the following:

    A 200 GRP1

    A 500 GRP2

    GRP1 B 0

    B GRP2 0

    Can anyone help? Thanks in advance.

    Sorry, I couldn't find the button skip yet, although there is an urgent need for such a feature in this forum.

    That is why another chance for you:

    SELECT

    a.PKEY

    GRP

    SUM (case

    When a.pkey = b.pkey

    can AMT 0 otherwise

    AMT end)

    Of

    TLEFT HAS

    Cross JOIN

    TRIGHT B

    where FILTER = "XXX".

    GROUP BY a.PKEY, GRP

    ORDER BY a.PKEY, GRP

    PKEY GRP AMT
    A GRP1 200
    A GRP2 500
    B GRP1 0
    B GRP2 0
  • outer join when there are several tables are involved

    Could not put up the question correctly to the last channel, my problem is with the join when there are several tables are involved, this is just one example of the task that I have to carry.

    Tab1 aura model id retailer_id information for all the weeks (from the first Monday) of the month of JUNE with cost and Helen

    Tab1

    model_id

    retailer_id

    sell_date

    cost

    Helene

    1

    12

    June 3, 13

    100

    40

    1

    12

    June 10, 13

    200

    20

    1

    12

    17 June 13

    300

    20

    1

    12

    24 June 13

    400

    20

    2

    12

    June 3, 13

    300

    10

    2

    12

    June 10, 13

    200

    20

    2

    12

    17 June 13

    300

    20

    2

    12

    24 June 13

    400

    20

    Tab2:

    each retailer belongs to a dealer, under the table has the same information

    retailer_id

    Dealer_id

    12

    100

    13

    100

    14

    101

    15

    101

    16

    101

    Tab 3

    There is a third layer where each dealership is having a garage band

    Dealer_id

    Dealer_group

    100

    1001

    101

    1001

    102

    2001

    103

    2001

    104

    3001

    105

    3001

    Tab4:

    Of this table for each model and dealer discount information for the month of June (every week)

    model_id

    Dealer_group

    discount_date

    discount

    1

    1001

    June 3, 13

    10

    1

    1001

    June 10, 13

    20

    1

    1001

    17 June 13

    10

    1

    1001

    24 June 13

    30

    2

    1001

    June 3, 13

    10

    2

    1001

    June 10, 13

    20

    2

    1001

    17 June 13

    10

    2

    1001

    24 June 13

    30

    3

    2001

    June 3, 13

    10

    3

    2001

    June 10, 13

    20

    3

    2001

    17 June 13

    10

    3

    2001

    24 June 13

    30

    Master_info:

    It's the main table which is the master table for model /retailer information

    Model_id

    retailer_id

    1

    12

    2

    12

    3

    12

    4

    12

    1

    13

    2

    13

    Output

    model_id

    retailer_id

    sell_date

    cost

    Helene

    Final (cost-helene-discount)

    1

    12

    June 3, 13

    100

    40

    50

    1

    12

    June 10, 13

    200

    20

    160

    1

    12

    17 June 13

    300

    20

    270

    1

    12

    24 June 13

    400

    20

    350

    2

    12

    June 3, 13

    300

    10

    280

    2

    12

    June 10, 13

    200

    20

    160

    2

    12

    17 June 13

    300

    20

    270

    2

    12

    24 June 13

    400

    20

    350

    3

    12

    June 3, 13

    0

    0

    0

    3

    12

    June 10, 13

    0

    0

    0

    3

    12

    17 June 13

    0

    0

    0

    3

    12

    24 June 13

    0

    0

    0

    4

    12

    June 3, 13

    0

    0

    0

    4

    12

    June 10, 13

    0

    0

    0

    4

    12

    17 June 13

    0

    0

    0

    4

    12

    24 June 13

    0

    0

    0

    1

    13

    June 3, 13

    0

    0

    0

    1

    13

    June 10, 13

    0

    0

    0

    1

    13

    17 June 13

    0

    0

    0

    1

    13

    24 June 13

    0

    0

    0

    2

    13

    June 3, 13

    0

    0

    0

    2

    13

    June 10, 13

    0

    0

    0

    2

    13

    17 June 13

    0

    0

    0

    1

    13

    24 June 13

    0

    0

    0

    For highted above records (model_id / retailer_id combination) there is no record in tab1 but they have entered in master_info then the recordings should come for all model_id/retailer_id with all the 0 values

    Hello

    Thanks for posting the sample data.

    It is unclear what dates you want to include in the output.  The following query shows how you can generate every Monday in a given range.  If you only want to include the dates that are actually present in tabl1 and/or tab4, you can simplify this a bit.

    WITH date_range AS

    (

    SELECT TRUNC (TO_DATE (' 3 June 2013', 'DD-Mon-YYYY'))

    , 'IW '.

    ) AS first_monday

    , TRUNC (TO_DATE (24 June 2013 ', 'DD-Mon-YYYY') + 6)

    , 'IW '.

    ) AS last_monday

    OF the double

    )

    all_mondays AS

    (

    First_monday SELECT + (7 * (LEVEL - 1)) AS sell_date

    OF date_range

    CONNECT BY LEVEL<= 1="" +="" (="" (last_monday="" -="">

    / 7

    )

    )

    SELECT mi.model_id

    mi.retailer_id

    am.sell_date

    , Cost of NVL (t1.cost, 0) AS

    , NVL (t1.rebat, 0) IN the refund

    , NVL (t1.cost, 0)

    -(NVL (t1.rebat, 0))

    + NVL (t4.discount, 0)

    ) AS final

    E master_info

    CROSS JOIN all_mondays am

    LEFT OUTER JOIN tab1 t1 ON t1.model_id = mi.model_id

    AND t1.retailer_id = mi.retailer_id

    AND t1.sell_date = am.sell_date

    LEFT OUTER JOIN tab2 t2 ON t2.retailer_id = mi.retailer_id

    LEFT OUTER JOIN tab 3 t3 ON t3.dealer_id = t2.dealer_id

    LEFT OUTER JOIN tab4 t4 ON t4.model_id = t1.model_id

    AND t4.dealer_group = t3.dealer_group

    AND t4.discount_date = t1.sell_date

    ORDER BY mi.retailer_id

    mi.model_id

    am.sell_date

    ;

    The results are not exactly what said you you wanted.  I suspect it's because of typos in that you posted.

Maybe you are looking for