Solved

SQL server joining multiple tables in a query

Posted on 2013-11-12
4
257 Views
Last Modified: 2013-11-12
Hello Experts, I'm working with 3 tables in SQL server which I need to join in a query. table1 is HEADER, table 2 is BOX, table 3 is Detail. The detail level has the most records which is 442, however when I do a join using the field which is available in all 3 tables I get 39000+ records back. It seems that the data is multiplied instead of being displayed on 1 line. I'm using inner join command to create the link. Can you please suggest other methods of joining and displaying the data properly? Attached is excel with the 3 tables extracted, the key field which unifies the 3 tables is “Supplier Document Number”
Thank you.
ImportTablesForLinking.xlsx
0
Comment
Question by:EddieIT
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39642180
>It seems that the data is multiplied instead of being displayed on 1 line.
The most common reason for this is that the JOIN ... ON clause is missing some of the columns that relate the two tables.

This article may help you..
SQL Server: Table Joins Explained!
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39642200
It is the Box and Detail tables that are causing the problem. You will need to join those two tables using the Box Number.

Select .....
from Header H
inner join Detail D on H.[SupplierDocumentNumber] = D.[SupplierDocumentNumber]
inner join Box B on D.[BoxNumber] = B.[BoxNumber]
0
 

Author Comment

by:EddieIT
ID: 39642233
Noted Buttercup1, can you explain on why?
0
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 39642289
Take the Supplier Document Number of 2013801488 as an example. In the Box table, this ID has 54 records, and in the Detail table it has 235 records. As in Jim's article he is quoting, an inner join on the Supplier Document Number between these tables will join each 2013801488 record in the Box table with each 2013801488 record in the Detail table, multiplying the records.

So, for the 2013801488 record in the Box table, each of the 54 rows has a unique Box Number, which can be joined to the same column in the Detail table. That way, although the records are still multiplied, you are multiplying 235 X 1, instead of 235 X 54. Obviously I haven't gone through the entire table. If you find that a Box Number can apply to several Supplier Document Numbers, then you should use both columns in your join, as below.

Select .....
from Header H
inner join Detail D on H.[SupplierDocumentNumber] = D.[SupplierDocumentNumber]
inner join Box B on D.[BoxNumber] = B.[BoxNumber] and D.[SupplierDocumentNumber] = B.[SupplierDocumentNumber]

I hope that makes sense. The quoted article does a nice job of explaining also.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using a hta file with MSSQL agent to schedule a script job 4 42
Amazon RDS migrate to SQL Server 3 26
sql server query 12 26
SQL Find Carriage Return and Delete it. 3 16
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question