Outer join, 3 tables

Please see the attached link for table structures and data of the sample.

There will be several lines in the table of SALES with different SALETYPEs. There may be several lines in the table RETURNS, corresponding to some of the points of SALE. RETURNS the table has a foreign key to the SALE. The report that I am trying to generate with a select sql statement must include a line for each type of sale (from table SALETYPE) and the sum of sale amounts corresponding to this type of sale of the SALES table. The table may not return the entries for all types of sales and the amount of Scripture must be reported as zero. The SQL I use is below, but does not work. I hope that I have explained this correctly. Please see the doc attached for typical data that I work with. Can someone please?

https://spreadsheets.Google.com/pub?key=0AnDmnUJXOChwdGF2ZjdjdHFtMGF1MkJ3MWtjQ2JfUmc & hl = in & Single = true & gid = 0 & output = html

Select SUM (s.amount) as sum_amount, t.sale_type from SALES s, SALETYPE t, r RETURNS
where s.id = r.id
and s.sale_type_id = t.id
T.sale_type group;

Published by: 801941 on October 12, 2010 15:02

Hello - the second displays almost had. what you want to do is joining several tables at once - impossible with the syntax of the oracle, but possible if you use ANSI SQL.

Wrote file afiedt.buf

  1  WITH s AS
  2   (SELECT 1 id, 1 sale_type_id, 10 amount   FROM dual
  3    UNION ALL
  4    SELECT 2, 1, 10  FROM dual
  5    UNION ALL
  6    SELECT 3, 2, 10   FROM dual
  7    UNION ALL
  8    SELECT 4, 2, 10   FROM dual
  9    UNION ALL
 10    SELECT 5, 3, 10    FROM dual
 11    UNION ALL
 12    SELECT 6, 4, 10 FROM dual),
 13  r AS
 14   (SELECT 2 sale_id   FROM dual
 15    UNION ALL
 16    SELECT 3 FROM dual
 17    UNION ALL
 18    SELECT 4 FROM dual),
 19  t AS
 20   (SELECT 1 sale_type_id, 100 sale_type    FROM dual
 21    UNION ALL
 22    SELECT 2, 200    FROM dual
 23    UNION ALL
 24    SELECT 3, 300    FROM dual
 25    UNION ALL
 26    SELECT 4, 400 FROM dual)
 27  SELECT nvl(SUM(s.amount),0) total_amount, t.sale_type
 28    FROM t
 29     join r on (1=1)
 30     left outer join s on (s.id = r.sale_id and s.sale_type_id = t.sale_type_id)
 31   --WHERE s.id(+) = r.sale_id
 32  --   AND s.sale_type_id(+) = t.sale_type_id
 33   GROUP BY t.sale_type
 34*  order by t.sale_type
SQL> /

TOTAL_AMOUNT  SALE_TYPE
------------ ----------
          10        100
          20        200
           0        300
           0        400

4 rows selected.

The two commented lines are what you are trying to achieve using the syntax of the oracle, but which will give you an error.

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

  • Outer join on tables

    Hello I am trying to improve my self, I am working on the user for example HR oracle tablespaces

    HR diagram
    http://img.Photobucket.com/albums/v346/satanix/hrschema.PNG

    Select * from
    employees a
    job_history.employee_id = a.employee_id left outer join job_history
    departments of outer join left on departments.department_id = a.department_id
    outer join left on jobs.job_id jobs = a.job_id

    There is no problem up to now, but when I add

    locations of outer join left on locations.location_id = departments.department_id
    country of outer join left on countries.country_id = locations.country_id
    regions of outer join left on regions.region_id = countries.region_id

    they came to the table with null values

    Please help thanks

    Select * from
    employees a
    job_history.employee_id = a.employee_id left outer join job_history
    departments of outer join left on departments.department_id = a.department_id
    outer join left on jobs.job_id jobs = a.job_id
    locations of outer join left on locations.location_id = departments.location_id
    country of outer join left on countries.country_id = locations.country_id
    regions of outer join left on regions.region_id = countries.region_id

    Probably the above query might work
    Please check the answer correct or useful, if it is

  • Join the tables in the responses

    I would like to know if it is possible to join the tables together as below in the answers?

    Select a.Field1, b.Field1
    FROM tableA an outer join left tableA b on
    a.Field1 = b.Field

    If you want to make an INNER JOIN, and then type INNER JOIN instead of LEFT INNER JOIN.

    If you want to left outer join and LEFT OUTER JOIN instead of INTERNAL LEFT JOIN type.

    Nothing asked as a left inner join. That is why it gives error + [nQSError: 27002] near +.

    Thank you

  • Using Left Outer Join with reference

    I have three tables.
    Table 1: BOOK_DETAILS
    Fields: BOOK_ID, BOOK_NAME

    Table 2: BOOK_ISSUE_RECORD
    Fields: BOOK_ID, USER_NAME

    Table 3: BOOK_AUTHOR
    Fields: BOOK_ID, AUTHOR_NAME

    I must link table 1 and table 2 with a left outer join, because even if the book is not the questions to anyone, his name should come.
    I have once again display the name of the author of books for each book.
    I am able to create a query with the left outer join between table 1 and table 2. However, I am not able to give a reference to Table 3.
    Can someone help me with this please.

    Concerning
    Hawker
    select  d.book_name,
            a.book_author,
            i.user_name
      from       book_details d
            join
                 book_author a
              on (d.book_id = a.book_id)
            left join
                 book_issue_recors i
              on (d.book_id = i.book_id)
    /
    

    SY.

  • using (+) or left outer join

    Dear Expert;

    I've been playing by using the two symbol... and realized that they do the same thing... Is it true...? or am I wrong.

    Thank you.

    Hello

    user13328581 wrote:
    Dear Expert;

    I've been playing by using the two symbol... and realized that they do the same thing... Is it true...? or am I wrong.

    They all have two outer joins. The + rating was the original way to do it in Oracle. LEFT, RIGHT, and FULL OUTER JOIN introduced in Oracle 9, but the old way is still supported.

    There are some situations (such as an outer join complete and outer-join a table to two different tables) that are better with the ANSI syntax (it's LEFT OUTER JOIN). It is possible to get the same results using +, but it must be combersome and/or inefficient workarounds. I suggest that you use always LEFT OUTER JOIN (or FULL OUTER JOIN, or, on occasions RIGHT OUTER JOIN). I think that you will find it easier and less error-prone.

  • 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

  • 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

  • Create a view which limits a large table, but also allows an outer join?

    Oracle 10.2.0.4

    CREATE TABLE MY_PAY_ITEMS

    (EMP VARCHAR2 (8) NOT NULL)

    , PAY_PRD VARCHAR2 (8) NOT NULL

    , KEY1 VARCHAR2 (8) NOT NULL

    , KEY2 VARCHAR2 (8) NOT NULL

    , LN_ITEM VARCHAR2 (4) NOT NULL

    , ITEM_AMT NUMBER (24.2) NOT NULL

    VARCHAR2 (100) FILL NON-NULL)

    INSERT INTO MY_PAY_ITEMS

    SELECT A.EMP

    B.PAY_PRD

    C.KEY1

    D.KEY2

    E.LN_ITEM

    F.ITEM_AMT

    , '                      '

    FROM (SELECT TO_CHAR (ROWNUM, ' 00000000') 'EMP' FROM DUAL CONNECT BY LEVEL < = 50) has

    , (SELECT '2010-' |) To_char(rowNum,'00') 'PAY_PRD' FROM DUAL CONNECT BY LEVEL < = 52) B

    , (SELECT TO_CHAR (ROWNUM, ' 000') 'KEY1' FROM DUAL CONNECT BY LEVEL < = 8) C

    , (SELECT TO_CHAR (ROWNUM, ' 000') 'Key2' FROM DUAL CONNECT BY LEVEL < = 5) D

    , (SELECT TO_CHAR(ROWNUM,'000') 'LN_ITEM' FROM DUAL CONNECT BY LEVEL < = 20) E

    (round (DBMS_RANDOM. VALUE * 400.2) "ITEM_AMT" of the double) F

    CREATE A UNIQUE INDEX MY_PAY_ITEMS ON MY_PAY_ITEMS (MEP, PAY_PRD, KEY1, KEY2, LN_ITEM)

    CREATE TABLE MY_ITEM_DISPLAY

    (THE VARCHAR2 (4) OF DISPLAY_CODE NOT NULL)

    , NUMBER (2) SEQUENCE NON-NULL

    , COLUMN_ITEM1 VARCHAR2 (4) not null

    , COLUMN_ITEM2 VARCHAR2 (4) not null

    , COLUMN_ITEM3 VARCHAR2 (4) not null

    COLUMN_ITEM4 VARCHAR2 (4) not null)

    INSERT INTO MY_ITEM_DISPLAY VALUES ('01 ', 10, '001', '003', '004',' 005');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('01 ', 20 '007', '013', '004',' 009');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('01', 30 ' 001', '004', '009',' 011');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('01', 40, '801 ', ' 304 ', 209',' 111');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('02 ', 10, '001', '003', '004',' 005');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('02 ', 20 '007', '013', '004',' 009');

    INSERT INTO MY_ITEM_DISPLAY VALUES ('02', 30 ' 001', '004', '009',' 011');

    MY_PAY_ITEMS is a table that stores line of payslip items.  It has a total area of 500,000,000 lines.

    PEM is the unique id, we have about 200,000 employees (with about 50,000 being active today).

    PAY_PRD is a weekly pointer (2010-01, 2010-02, 2010-52), we have the 2004 data and add a new pay period each week.  2010 01 is defined as the first Monday in 2010 for the first Sunday in 2010, etc.

    Key1 is an internal key, it follows the chronology in the pay period.

    KEY2 is a child of KEY1, there follows the sequence of events in KEY1.

    LN_ITEM is the component of actual compensation resulting from the event, on average, a person produces 20 lines per event.  Note that in this example, everyone gets the same LN_ITEM values, but in practice it is selected 20 of 300

    ITEM_AMT is the net salary per command line.

    FILLING is a set of fields that are unrelated to this issue, but act as a brake on the charges of the line.

    MY_ITEM_DISPLAY is an array that describes how some screens should display items.  The screen itself is a grid of 4 poles, with the content of each cell is defined as a LN_ITEMS search to retrieve the relevant LN_AMT.

    We have an application that receives a DISPLAY_CODE and an EMP.  It automatically creates an sql statement in the sense of

    SELECT * FROM MY_VIEW WHERE DISPLAY_CODE =: 1 AND EMP =: 2

    and returns the output to the user.

    My challenge is that I need to rewrite MY_VIEW as follows:

    (1) (select the relevant lines from MY_ITEM_DISPLAY where DISPLAY_CODE =: 1)

    (2) select the relevant all MY_PAY_ITEMS lines that meet the criteria

    (a) EMP =: 2

    (b) PAY_PRD = (a more recent for EMP and sysdate, so if he got last paid 2010-04, return 2010-04)

    (c) KEY1 = (highest key1 in EMP and PAY_PRD)

    (d) KEY2 = (highest key2 in KEY1, PAY_PRD and EMP)

    (3) I then need to cross reference to create tabular output

    (4) Finally, I have to return a 0 line where there is no LN_ITEMs (DISPLAY_CODE 01, 40 sequence contains possible values for this scenario)

    The following query is part of it (but not the PAY_PRD, KEY1, KEy2)

    Select * from)

    SELECT A.DISPLAY_CODE

    B.EMP

    A.SEQUENCE

    , MAX (DECODE (B.LN_ITEM, A.COLUMN_ITEM1, B.ITEM_AMT, 0)) 'COL1 '.

    , MAX (DECODE (B.LN_ITEM, A.COLUMN_ITEM2, B.ITEM_AMT, 0)) 'COL2 '.

    , MAX (DECODE (B.LN_ITEM, A.COLUMN_ITEM3, B.ITEM_AMT, 0)) 'COL3 '.

    , MAX (DECODE (B.LN_ITEM, A.COLUMN_ITEM4, B.ITEM_AMT, 0)) 'COL4 '.

    OF MY_ITEM_DISPLAY A, MY_PAY_ITEMS B

    WHERE B.PAY_PRD = ' 2010-03'

    GROUP OF A.DISPLAY_CODE, B.EMP, A.SEQUENCE)

    WHERE DISPLAY_CODE = '01'

    AND EMP = '0000011'

    SEQUENCE ORDER

    My questions

    (1) how I do PAY_PRD, KEY1, KEY2 forced, can I use a form any of ROW_NUMBER() FUNCTION?

    (2) how can I deal with the fact that none of the 4 LN_ITEMS column can exist (see sequence 40, none of these can exist)...  Ideally the SQL above should return

    01, 0000011, 10, < a digit - > < number > - < a digit - > < some number >

    01, 0000011, 20, < a digit - > < number > - < a digit - > < some number >

    01, 0000011, 30, < a number--> < number > - < number > - < number > -

    01, 0000011, 40, 0, 0, 0, 0

    I tried a UNION, but it prevented the view to eliminate most of the MY_PAY_ITEMS lines, because it solves ALL MY_PAY_ITEMS instead of simply retrieve lines for the EMP passed to the view.  The same seems to be true for outer joins.

    Hi Paula,

    It does not handle SEQNUM 40, it's just that your second set of test data did not SEQNUM 40. When I put in the first set of values MY_ITEM_DISPLAY, I get:

    DISP EMP SEQUENCE COL1 COL2 COL3 COL4

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

    01 00000011 10 101.1 103.1 105.1 104.1

    01 00000011 20 107.1 113.1 104.1 109,1

    01 00000011 30 101.1 104.1 109.1 99.99

    01 00000011 40 0 0 0 0

    If this isn't the issue, you will need to explain the requirement of "outer join" and what you want more. Apart from his return nothing if there is absolutely no element of remuneration of the employee, this query will always return a line for each line MY_ITEM_DISPLAY for the display_code. If there is absolutely no data, no return line seems reasonable.

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

  • Outer join - problem with the name of the table in the select list

    Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production

    create table (j1)

    number of C1,

    number of C2);

    create table (j2)

    number of C1,

    number of C2);

    insert into values j1 (1, 10);

    insert into j1 values (1, 100);

    insert into values j1 (1, 1000);

    insert into values j2 (1, 2);

    insert into values j2 (1, 4);

    insert into values j2 (1, 8);

    commit;

    Select c1, j1.c2, j2.c2 of outer join of j1 j2 using (c1); - DOES NOT

    Select c1, j1 j2 (c1) using outer join j2.c2. - WORK

    Why?

    Hello

    Interesting question!  Oracle goes very far in trying not to trigger an error.

    The OUTER keyword (if used; it is always optional) must be preceded by one of the keywords, right or LEFT.  Since neither LEFT, RIGHT, or FULL comes before OUTER in queries in your first message, it does not recognize as keyword OUTER and treats him like a table alias for table j1.  Since j1 has an alias, the real table name cannot be used in the SELECT clause.  This query is executed:

    SELECT c1

    outer.c2

    j2.c2

    External J1

    Join the HELP of j2 (c1)

    ;

    There is an INTERNAL join.  Add to your sample data:

    insert into values of j1 (-1, -10);

    insert into values of j2 (-2, -20);

    to be specified.

    Moreover, USING is short for confUSING.  I suggest you use IT for all the join conditions.  Everyone (unless they you write or read a manual) does.

  • 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')
    
  • Outer Join and joining several Tables

    Hello

    Oracle Database 10 g Express Edition Release 10.2.0.1.0 - product

    I have three tables AddProject, AssociateProjectLead, AddAssociate. Now I'm generating a report who will join AddProject and AssociateProjectLead for a list of all the projects, even if it doenst have a project coordinator. I used the outer join for this. However, if I want the name of the project leader, I need to search for AddAssociate table. By joining this table also, the outer join is no longer valid. Please see the result below

    CREATE TABLE  "ADDPROJECT" 
       (     "VERSIONNO" NUMBER(*,0), 
         "PROJID" VARCHAR2(20), 
         "PROJNAME" VARCHAR2(60), 
         "PROJSTARTDATE" DATE, 
         "PROJSTATUS" VARCHAR2(20), 
         "PROJENDDATE" DATE, 
         "PROJENDTYPE" VARCHAR2(20), 
         "PROJENDREASON" VARCHAR2(1000), 
         "UCPROJECTMANAGER" VARCHAR2(20), 
         "FROMDATE" DATE, 
         "TODATE" DATE, 
         "SRCHFIELD" VARCHAR2(20), 
         "OPERATOR" VARCHAR2(20), 
         "PARENTPROJID" VARCHAR2(20), 
         "PROJHIDDENDATE" VARCHAR2(20), 
          CONSTRAINT "PK_B36" PRIMARY KEY ("PROJID", "PROJHIDDENDATE") ENABLE
       )
    
    
    CREATE TABLE  "ADDASSOCIATE" 
       (     "VERSIONNO" NUMBER(*,0), 
         "ASSOCIATEID" NUMBER(9,0) NOT NULL ENABLE, 
         "ASSOCIATENAME" VARCHAR2(100) NOT NULL ENABLE, 
         "CTOJOINDATE" DATE, 
         "STATUS" VARCHAR2(20), 
         "ENDDATE" DATE, 
         "SRCHFIELD" VARCHAR2(20), 
         "OPERATOR" VARCHAR2(20), 
         "FROMDATE" DATE, 
         "TODATE" DATE, 
          CONSTRAINT "PK_B23" PRIMARY KEY ("ASSOCIATEID") ENABLE
       )
    
    CREATE TABLE  "ASSOCIATEPROJECTLEAD" 
       (     "VERSIONNO" NUMBER(*,0), 
         "PROJLEADASSOID" NUMBER(9,0), 
         "PROJID" VARCHAR2(20), 
         "ASSOCIATEID" NUMBER(9,0), 
         "PROJLEADSTARTDATE" DATE, 
         "STATUS" VARCHAR2(20), 
         "ASSOCPROJHIDDENDATE" VARCHAR2(20), 
         "PROJHIDDENDATE" VARCHAR2(20), 
         "ENDDATE" DATE, 
          CONSTRAINT "PK_B40" PRIMARY KEY ("ASSOCIATEID", "PROJID", "ASSOCPROJHIDDENDATE") ENABLE
       )
    
    
    elect ap.projid,apl.associateid
    from addproject ap, associateprojectlead apl
    where ap.projid = apl.projid(+)
    and ap.projhiddendate = apl.projhiddendate(+)
    
    
    PROJID ASSOCIATEID 
    Proj08 75825 
    Proj09 75825 
    Proj10 75825 
    Proj11 75825 
    Proj12 259811 
    Proj01 103035 
    Proj02 103035 
    Proj03 320092 
    Proj04 320092 
    Proj05 120974 
    Proj06 367393 
    Proj07 117618 
    Proj07 224882 
    Proj07 246652 
    prj001 -  
    prj001 -  
    
    16 rows returned in 0.00 seconds
    
    
    select ap.projid,apl.associateid,aa.associatename
    from addproject ap, associateprojectlead apl,addassociate aa
    where ap.projid = apl.projid(+)
    and ap.projhiddendate = apl.projhiddendate(+)
    and apl.associateid = aa.associateid
    
    
    PROJID ASSOCIATEID ASSOCIATENAME 
    Proj11 75825 Amarendra Kumar Singh 
    Proj10 75825 Amarendra Kumar Singh 
    Proj09 75825 Amarendra Kumar Singh 
    Proj08 75825 Amarendra Kumar Singh 
    Proj02 103035 Rajesh Jayaprakash 
    Proj01 103035 Rajesh Jayaprakash 
    Proj07 117618 Chetan Malhotra 
    Proj05 120974 Perumal Rajaram 
    Proj07 224882 Dilshad Ahmad 
    Proj07 246652 Shankar Kausley 
    Proj12 259811 Arunchandar Arun Vasan 
    Proj04 320092 Venkatesh Sarangan 
    Proj03 320092 Venkatesh Sarangan 
    Proj06 367393 Venkata Ramakrishna P 
    
    14 rows returned in 0.00 seconds
    How can I select all the values in table AddProject?

    Published by: Pramukh on August 23, 2012 12:18

    Hello

    I could get the result with a bit of modification

    select ap.projid,
           apl.associateid,
           aa.associatename
      from addproject ap
           left outer join
           associateprojectlead apl
        on ap.projid = apl.projid
       and ap.projhiddendate = apl.projhiddendate
           left outer join
           addassociate aa
        on apl.associateid = aa.associateid
    

    As a follow-up, I have a request more. In the report form, the user can select a particular project ID and the report should be generated as a result. For example;-he wants to see the results of the only "Proj08". I get the output as below, while the result should display only the details of 'Proj08 '.

    select ap.projid,
           apl.associateid,
           aa.associatename
      from addproject ap
           left outer join
           associateprojectlead apl
        on ap.projid = apl.projid
       and ap.projhiddendate = apl.projhiddendate
       and ap.projID = 'Proj08'
           left outer join
           addassociate aa
        on apl.associateid = aa.associateid
    ORDER BY ap.projID
    
    PROJID ASSOCIATEID ASSOCIATENAME
    Proj01 -  -
    Proj02 -  -
    Proj03 -  -
    Proj04 -  -
    Proj05 -  -
    Proj06 -  -
    Proj07 -  -
    Proj08 75825 Amarendra Kumar Singh
    Proj09 -  -
    Proj10 -  -
    Proj11 -  -
    Proj12 -  -
    prj001 -  -
    prj001 -  -  
    
  • 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?)

Maybe you are looking for