Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL server joining multiple tables in a query

Posted on 2013-11-12
4
Medium Priority
?
262 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 66

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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.
Viewers will learn how the fundamental information of how to create a table.

876 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