Stoating
Stoating

Reputation: 411

Join More Than 2 Tables

I have three tables.

So the "Files" table looks like this. As you can see it is a listing of each file that exists. The LimitsA file will contain the limits for every Data file of type A.

ID  File_Name  Type  Sub-Type
1   DataA_10   A     10
2   DataA_20   A     20
3   DataA_30   A     30
4   LimitsA    A     NONE
5   DataB_10   B     10
6   DataB_20   B     20
7   LimitsB    B     NONE

The "Data" table looks like this. The File_ID is the foreign key from the "Files" table. Specifically, this would be data for DataA_10 above:

ID  File_ID  Dat1  Dat2  Dat3... Dat20
1   1        50    52    53 
2   1        12    43    52 
3   1        32    42    62 

The "Limits" table looks like this. The File_ID is the foreign key from the "Files" table. Specifically, this would be data for LimitsA above:

ID  File_ID  Sub-Type  Lim1  Lim2
1   4        10        40    60    
2   4        20        20    30    
3   4        30        10    20    

So what I want to do is JOIN the correct limits from the "Limit" table to the data from the corresponding "Data" table. Each row of DataA_10 would have the limits of "40" and "60" from the LimitsA table. Unfortunately there is no way to directly link the limits table to the data table. The only way to do this would be to look back to the files table and see that LimitsA and DataA_10 are of type A. Once I link those two together I then need to specifically only grab the Limits for Sub-Type 10.

In the end I would like to have a result that looks like this.

Result:

ID  File_ID  Dat1  Dat2  Dat3... Dat20  Lim1  Lim2
1   1        50    52    53             40    60
2   1        12    43    52             40    60
3   1        32    42    62             40    60

I hope this is clear enough to understand. It seems to me like an issue of joining more than 2 tables, but I have been unable to find a suitable solution online as of yet. If you have a solution or any advice it would be greatly appreciated.

Upvotes: 2

Views: 468

Answers (1)

PinnyM
PinnyM

Reputation: 35533

Your 'Files' table is actually 2 separate (but related) concepts that have been merged. If you break them out using subqueries you'll have a much easier time making a join. Note that joining like this is not the most efficient method, but then again neither is the given schema...

SELECT Data.*, Limits.Lim1, Limits.Lim2
FROM (SELECT * FROM Files WHERE SubType IS NOT NULL) DataFiles
JOIN (SELECT * FROM Files WHERE SubType IS NULL) LimitFiles
  ON LimitFiles.Type = DataFiles.Type
JOIN Data 
  ON DataFiles.ID = Data.File_ID
JOIN Limits
  ON LimitFiles.ID = Limits.File_ID
  AND DataFiles.SubType = Limits.SubType
ORDER BY Data.File_ID

UPDATE

To be more specific on how to improve the schema: Currently, the Files table doesn't have a clear way to differentiate between Data and Limit file entries. Aside from this, the Data entries don't have a clear link to a single Limit file entry. Although both of these can be figured out as in the SQL above, such logic might not play well with the query optimizer, and certainly can't guarantee the Data-Limit link that you require.

Consider these options:

  • Instead of linking to a 'Limit' file via Type, link directly to a Limit entry Id. Set a foreign key on that link to ensure the expected Limit entry is available.
  • Separate the 'Limit' entries from the 'Data' entries by putting them in a separate table.
  • Create an index on the foreign key. For that matter, add indices for all foreign keys - SQL Server doesn't do this by default.

Of these, I would consider having a foreign key as essential, and the others as modest improvements.

Upvotes: 1

Related Questions