Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to join four tables using left outer join


How to join four tables using left outer join

Author
Message
neellotus07
neellotus07
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 157
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8964 Visits: 19020
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
neellotus07
neellotus07
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 157
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8964 Visits: 19020
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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14956 Visits: 38953
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!

Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8964 Visits: 19020
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
drew.allen
drew.allen
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2645 Visits: 9917
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search