October 8, 2012 at 3:17 am
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
October 8, 2012 at 3:30 am
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
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
October 8, 2012 at 4:58 am
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
October 8, 2012 at 5:20 am
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.
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
October 8, 2012 at 6:36 am
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
October 8, 2012 at 7:32 am
Doesn't look like SQL query to me. What curve-brackets are doing there?
October 9, 2012 at 1:25 am
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?
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
October 12, 2012 at 2:26 pm
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