SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Join Multiple Tables


Join Multiple Tables

Author
Message
nfpacct
nfpacct
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 194
If have 6 tables.
tblPRTS is my master table which has 1 to many relationships to 5 other tables.
To test my join statemnet I created the following query to retrieve the key field from each table. based on a single value in tblPRTS.
Although I am able to retrieve the key column from each table, the number of records is multiplied. I get 20 records instead of 5 which is the maximum number of records in tblPRTSBene.


select a.dtimesheetid,b.dtimesheetid,c.dtimesheetid, d.dtimesheetid,e.dtimesheetid,f.dtimesheetid
from tblPRTS as a
join tblPRTSEarn as b on a.dTimesheetID = b.dTimesheetID
join tblPRTSDeduct as c on a.dTimesheetID = c.dtimesheetid
join tblPRTSBene as d on a.dTimesheetID = d.dtimesheetid
join tblPRTSLeave as e on a.dTimesheetID = e.dtimesheetid
join tblPRTSWC as f on a.dTimesheetID = f.dtimesheetid
where a.dTimesheetID = 40827.651917091
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61681 Visits: 17954
You probably have more than 1 record for each ID. What are the counts in each of those tables for that ID?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
nfpacct
nfpacct
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 194
Sean,
you are correct some of the tables have a multiple records.

Does the query syntax correct for joining multiple tables.

select a.dtimesheetid,b.dtimesheetid,c.dtimesheetid, d.dtimesheetid,e.dtimesheetid,f.dtimesheetid
from tblPRTS as a (1 Record - Master Record)

join tblPRTSEarn as b on a.dTimesheetID = b.dTimesheetID (1 Record - but can be 1 or many)
join tblPRTSDeduct as c on a.dTimesheetID = c.dtimesheetid (2 Records - but can be 1 or many)
join tblPRTSBene as d on a.dTimesheetID = d.dtimesheetid (5 Records - but can be 1 or many)
join tblPRTSLeave as e on a.dTimesheetID = e.dtimesheetid(2 Records - but can be 1 or many)
join tblPRTSWC as f on a.dTimesheetID = f.dtimesheetid (1 Records - but can be 1 or many)

where a.dTimesheetID = 40827.651917091 and a.sTimesheetType = 'D'
KrishDBA
KrishDBA
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 80
The query is correct for joining multiple tables. As you have multiple records in the child tables, the result set eventually will have multiple records.

try putting a DISTINCT in the query.
KrishDBA
KrishDBA
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 80
Also i see there is no need for the selecting all id column of all the child tables:
select a.dtimesheetid,b.dtimesheetid,c.dtimesheetid, d.dtimesheetid,e.dtimesheetid,f.dtimesheetid

since a.dtimesheetid = b.dtimesheetid, both are same in the output.
Also, a.dtimesheetid = c.dtimesheetid, then both are same.

So, instead of writing
select a.dtimesheetid,b.dtimesheetid,c.dtimesheetid, d.dtimesheetid,e.dtimesheetid,f.dtimesheetid
you can write like
select DISTINCT a.dtimesheetid
FROM
......


Hope this helped you.
toddasd
toddasd
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1298 Visits: 3820
nfpacct (11/9/2011)
Sean,
you are correct some of the tables have a multiple records.

Does the query syntax correct for joining multiple tables.

select a.dtimesheetid,b.dtimesheetid,c.dtimesheetid, d.dtimesheetid,e.dtimesheetid,f.dtimesheetid
from tblPRTS as a (1 Record - Master Record)

join tblPRTSEarn as b on a.dTimesheetID = b.dTimesheetID (1 Record - but can be 1 or many)
join tblPRTSDeduct as c on a.dTimesheetID = c.dtimesheetid (2 Records - but can be 1 or many)
join tblPRTSBene as d on a.dTimesheetID = d.dtimesheetid (5 Records - but can be 1 or many)
join tblPRTSLeave as e on a.dTimesheetID = e.dtimesheetid(2 Records - but can be 1 or many)
join tblPRTSWC as f on a.dTimesheetID = f.dtimesheetid (1 Records - but can be 1 or many)

where a.dTimesheetID = 40827.651917091 and a.sTimesheetType = 'D'



Sure, the query is syntactically correct, but logically not what you are looking for. Notice it will join the IDs where they match for each subordinate table. With 2 rows in tblPRTSDeduct and 5 in tblPRTSBene, there will be 10 combinations. The total number of combinations for your joins will be 1x2x5x2x1=20 in this example. If you want a listing of all subordinate rows that match the one master row, use unions to select from each of the subordinate tables separately.

______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
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