How to join four tables using left outer join

  • Hi All,

    I want to show data from four tables on the basis of left outer join.All tables contain common column "a_code" on which i want to apply left outer join.

    Pls tell me correct syntax to join these tables i will be very thankfull to you.

    thanx

    Neel

  • neellotus07 (10/8/2012)


    Hi All,

    I want to show data from four tables on the basis of left outer join.All tables contain common column "a_code" on which i want to apply left outer join.

    Pls tell me correct syntax to join these tables i will be very thankfull to you.

    thanx

    Neel

    Here's the simplest example, three tables all left joined to one. It assumes that Table1 contains all of the values of a_code which will be encountered in the other three tables.

    SELECT something

    FROM Table1 t1

    LEFT OUTER JOIN table2 t2 ON t2.a_code = t1.a_code

    LEFT OUTER JOIN table3 t3 ON t3.a_code = t1.a_code

    LEFT OUTER JOIN table4 t4 ON t4.a_code = t1.a_code

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi Chris,

    Firstly thanx for your reply.

    I have written below mentioned query that contain six tables:-

    LEFT OUTER JOIN on PROD_REQR with three tables (MTP_ISSUE, MTP_TSTK_A & MTP_FGSTK_A ).

    EQUAL JOIN on PROD_REQR with two tables (MPC_A & BND_AGNT).

    SELECT MPC_A."DOC_DT", MPC_A."DOC_NO", PROD_REQR."doc_no", PROD_REQR."doc_dt", PROD_REQR."a_code", PROD_REQR."p_reqr",

    PROD_REQR."p_stk", MTP_ISSUE."issue", bnd_agnt."Frst_bnd", MTP_TSTK_A."RAWQTY", MTP_FGSTK_A."PEND_QTY"

    FROM { oj (((("SFNData"."dbo"."MPC_A" MPC_A

    INNER JOIN "SFNData"."dbo"."PROD_REQR" PROD_REQR ON MPC_A."DOC_NO" = PROD_REQR."doc_no"

    AND MPC_A."DOC_DT" = PROD_REQR."doc_dt") INNER JOIN "SFNData"."dbo"."bnd_agnt" bnd_agnt ON PROD_REQR."a_code" = bnd_agnt."i_code")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_TSTK_A" MTP_TSTK_A ON PROD_REQR."a_code" = MTP_TSTK_A."A_CODE")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_FGSTK_A" MTP_FGSTK_A ON PROD_REQR."a_code" = MTP_FGSTK_A."A_CODE")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_ISSUE" MTP_ISSUE ON PROD_REQR."doc_dt" = MTP_ISSUE."doc_dt" AND PROD_REQR."a_code" = MTP_ISSUE."a_code"}

    ORDER BY PROD_REQR."doc_no" ASC

    I am using this query to create crystal report but it is hanging my report while running.

    Pls tell me all joins are right or wrong or some updation required.

    thanx in advance

    neel

  • Is this query generated by Crystal Reports?

    You need to know what the relationships are between the tables. If you don't have an ERD (Entity Relationship Diagram), request one from your DBA. It's quite feasible to determine approximately what the relationships might be but it can be painstaking, and it will make assumptions.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • just formatted for readability:

    SELECT

    MPC_A."DOC_DT",

    MPC_A."DOC_NO",

    PROD_REQR."doc_no",

    PROD_REQR."doc_dt",

    PROD_REQR."a_code",

    PROD_REQR."p_reqr",

    PROD_REQR."p_stk",

    MTP_ISSUE."issue",

    bnd_agnt."Frst_bnd",

    MTP_TSTK_A."RAWQTY",

    MTP_FGSTK_A."PEND_QTY"

    FROM { oj (((("SFNData"."dbo"."MPC_A" MPC_A

    INNER JOIN "SFNData"."dbo"."PROD_REQR" PROD_REQR ON MPC_A."DOC_NO" = PROD_REQR."doc_no"

    AND MPC_A."DOC_DT" = PROD_REQR."doc_dt") INNER JOIN "SFNData"."dbo"."bnd_agnt" bnd_agnt ON PROD_REQR."a_code" = bnd_agnt."i_code")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_TSTK_A" MTP_TSTK_A ON PROD_REQR."a_code" = MTP_TSTK_A."A_CODE")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_FGSTK_A" MTP_FGSTK_A ON PROD_REQR."a_code" = MTP_FGSTK_A."A_CODE")

    LEFT OUTER JOIN "SFNData"."dbo"."MTP_ISSUE" MTP_ISSUE ON PROD_REQR."doc_dt" = MTP_ISSUE."doc_dt" AND PROD_REQR."a_code" = MTP_ISSUE."a_code"}

    ORDER BY

    PROD_REQR."doc_no" ASC

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Doesn't look like SQL query to me. What curve-brackets are doing there?

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (10/8/2012)


    Doesn't look like SQL query to me. What curve-brackets are doing there?

    It looks vaguely familiar - possibly the output of the Crystal query designer?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • neellotus07 (10/8/2012)


    I am using this query to create crystal report but it is hanging my report while running.

    Pls tell me all joins are right or wrong or some updation required.

    If you're using this in Crystal Reports, the problem might be that you should be using sub-reports instead of trying to cram this all into the main report. It's likely that you're getting a cross join on the various outer tables, causing your performance problems, and putting these into separate sub-reports should alleviate that problem.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply