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

Struggling on Duplicates while Join Three Table Produce Expand / Collapse
Author
Message
Posted Tuesday, August 12, 2014 2:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 11, 2014 2:26 AM
Points: 19, Visits: 109
Hi, I need help on join three tables and wont be duplicate records.

I have tried and attached the computed results and also expecting results.

IF OBJECT_ID('tempdb..#tmpExam1')IS NOT NULL DROP TABLE #tmpExam1
IF OBJECT_ID('tempdb..#tmpExam2')IS NOT NULL DROP TABLE #tmpExam2
IF OBJECT_ID('tempdb..#tmpExam3')IS NOT NULL DROP TABLE #tmpExam3


CREATE TABLE #tmpExam1(
Place Varchar(MAX),
Objective Varchar(Max),
Year INT,
Month INT,
Exam1 BIGINT
)
CREATE TABLE #tmpExam2(
Place Varchar(MAX),
Objective Varchar(Max),
Year INT,
Month INT,
Exam2 BIGINT
)
CREATE TABLE #tmpExam3(
Place Varchar(MAX),
Objective Varchar(Max),
Year INT,
Month INT,
Exam3 BIGINT
)

Insert into #tmpExam1 Values('Local','Section 1(a)',2010,1,55)
Insert into #tmpExam1 Values('Local','Section 1(b)',2010,1,68)
Insert into #tmpExam1 Values('Local','section 2(a)',2010,1,91)
Insert into #tmpExam1 Values('Local','Section 2(b)',2010,1,120)


Insert into #tmpExam2 Values('Local','Section 1(a)',2010,1,'')
Insert into #tmpExam2 Values('Local','Section 1(b)',2010,1,'')
Insert into #tmpExam2 Values('Local','section 2(a)',2010,1,'')
Insert into #tmpExam2 Values('Local','Section 2(b)',2010,1,'')


Insert into #tmpExam3 Values('Local','Section 1(a)',2010,1,55)
Insert into #tmpExam3 Values('Local','Section 1(b)',2010,1,68)
Insert into #tmpExam3 Values('Local','section 2(a)',2010,1,91)
Insert into #tmpExam3 Values('Local','Section 2(b)',2010,1,120)

SELECT E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3 FROM #tmpExam1 E1
LEFT JOIN #tmpExam2 E2 ON E2.Year = E1.Year and E2.Month = E2.Month
INNER JOIN #tmpExam3 E3 ON E3.Year = E1.Year and E3.Month = E2.Month
Group by E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3



Above Query Results are
============ =====
Place Objective Year Month Exam1 Exam2 Exam3
Local Section 1(a) 2010 1 55 0 55
Local Section 1(a) 2010 1 55 0 68
Local Section 1(a) 2010 1 55 0 91
Local Section 1(a) 2010 1 55 0 120
Local Section 1(b) 2010 1 68 0 55
Local Section 1(b) 2010 1 68 0 68
Local Section 1(b) 2010 1 68 0 91
Local Section 1(b) 2010 1 68 0 120
Local section 2(a) 2010 1 91 0 55
Local section 2(a) 2010 1 91 0 68
Local section 2(a) 2010 1 91 0 91
Local section 2(a) 2010 1 91 0 120
Local Section 2(b) 2010 1 120 0 55
Local Section 2(b) 2010 1 120 0 68
Local Section 2(b) 2010 1 120 0 91
Local Section 2(b) 2010 1 120 0 120



Expecting Results :
==============
Place Objective Year Month Exam1 Exam2 Exam3
Local Section 1(a) 2010 1 55 0 55
Local Section 1(b) 2010 1 68 0 68
Local section 2(a) 2010 1 91 0 91
Local Section 2(b) 2010 1 120 0 120

Thanks in advance

Post #1602188
Posted Tuesday, August 12, 2014 4:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:10 AM
Points: 9, Visits: 68
If u need that result ,u have to change the joins AS I mention in below

SELECT E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3 FROM #tmpExam1 E1
LEFT JOIN #tmpExam2 E2 ON E2.Year = E1.Year and E2.Month = E1.Month and E1.Objective =E2.Objective
Inner JOIN #tmpExam3 E3 ON E3.Year = E1.Year and E3.Month = E2.Month and E1.Objective =E3.Objective
Group by E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3
Post #1602210
Posted Tuesday, August 12, 2014 4:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:10 AM
Points: 9, Visits: 68
If u need that result ,u have to change the joins AS I mention in below

SELECT E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3 FROM #tmpExam1 E1
LEFT JOIN #tmpExam2 E2 ON E2.Year = E1.Year and E2.Month = E1.Month and E1.Objective =E2.Objective
Inner JOIN #tmpExam3 E3 ON E3.Year = E1.Year and E3.Month = E2.Month and E1.Objective =E3.Objective
Group by E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3
Post #1602211
Posted Tuesday, August 12, 2014 4:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:10 AM
Points: 9, Visits: 68
If u need that result ,u have to change the joins AS I mention in below

SELECT E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3 FROM #tmpExam1 E1
LEFT JOIN #tmpExam2 E2 ON E2.Year = E1.Year and E2.Month = E1.Month and E1.Objective =E2.Objective
Inner JOIN #tmpExam3 E3 ON E3.Year = E1.Year and E3.Month = E2.Month and E1.Objective =E3.Objective
Group by E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3[/quote][left][/left]
Post #1602212
Posted Tuesday, August 12, 2014 5:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:38 AM
Points: 6,751, Visits: 13,899
SELECT 
e1.Place,
e1.Objective,
e1.[Year],
e1.[Month],
e1.Exam1,
e2.Exam2,
e3.Exam3
FROM #tmpExam1 e1
LEFT JOIN #tmpExam2 e2
ON e2.Place = e1.Place
AND e2.[Year] = e1.[Year]
AND e2.Objective = e1.Objective
AND e2.[Month] = e2.[Month]
INNER JOIN #tmpExam3 e3
ON e3.Place = e1.Place
AND e3.Objective = e1.Objective
AND e3.[Year] = e1.[Year]
AND e3.[Month] = e2.[Month]
--Group by E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3
ORDER BY
e1.Place,
e1.Objective,
e1.[Year],
e1.[Month],
e1.Exam1,
e2.Exam2,
e3.Exam3



“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 #1602231
Posted Tuesday, August 12, 2014 6:38 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, September 25, 2014 4:56 AM
Points: 983, Visits: 289
HI

Try this method:


SELECT E1.Place, E1.Objective,E1.Year,E1.Month,E1.Exam1, E2.Exam2,E3.Exam3
FROM #tmpExam1 E1
inner join #tmpExam2 E2 on E1.objective = E2.objective
inner join #tmpExam3 E3 on E1.objective = E3.objective

Post #1602249
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse