Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to join four tables using left outer join Expand / Collapse
Author
Message
Posted Monday, October 8, 2012 3:17 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 31, 2013 2:00 AM
Points: 21, Visits: 150
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
Post #1369680
Posted Monday, October 8, 2012 3:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1369687
Posted Monday, October 8, 2012 4:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 31, 2013 2:00 AM
Points: 21, Visits: 150
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
Post #1369714
Posted Monday, October 8, 2012 5:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1369723
Posted Monday, October 8, 2012 6:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:23 AM
Points: 12,915, Visits: 32,074
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1369774
Posted Monday, October 8, 2012 7:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:10 AM
Points: 2,848, Visits: 5,092
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1369806
Posted Tuesday, October 9, 2012 1:25 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1370193
Posted Friday, October 12, 2012 2:26 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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
Post #1372357
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse