简体   繁体   中英

Joining two different tables with a common third table on a common column

Here are the tables

Table: Status

ID, StatusDesc
1,  New
2,  Active
3,  Cancelled
4,  Complete

Table: Order (foreign key relationship with Status table above)

ID, OrderNumber, StatusID
1,  1001 , 1
2,  1002,  1
3,  1003, 2
4,  1004, 3
5,  1500, 4

Table: LineItem(foreign key relationship with Order and Status tables above)

ID, OrderNumber, LineItemNumber, StatusID
1,  1001 , 1, 1
2,  1001 , 2, 1
3,  1002 , 1, 2
4,  1002 , 2, 1
5,  1003 , 1, 2
6,  1004 , 1, 3
7,  1004 , 2, 4
8,  1500 , 1, 3

As you can see, the table Status holds the statuses common for both Order and LineItem tables.

I want to produce the result which will include columns like this, status description for both Order and LineItem:

OrderNumber, LineItemNumber, StatusDesc_Order, StatusDesc_LineItem

How to do this?

You could join the Status table twice to achieve this:

SELECT
           o.OrderNumber
         , li.LineItemNumber
         , orderStatus.StatusDesc    AS StatusDesc_Order
         , lineItemStatus.StatusDesc AS StatusDesc_LineItem
FROM       [LineItem] AS li
INNER JOIN [Status]   AS lineItemStatus ON li.StatusID = lineItemStatus.ID
INNER JOIN [Order]  AS o ON li.OrderNumber = o.OrderNumber
INNER JOIN [Status]   AS orderStatus ON o.StatusID = orderStatus.ID

I do suggest however you try and stay away from table names using reserved keywords like Order and Status, it also is good practice to explcitly add schema prefixes before the table names in the query (ie dbo.Status or another user defined schema).

If the required results really are that simple then just use a couple of sub-queries eg

-- SETUP TEST DATA

declare @Order table (id int, OrderNumber int, StatusId int)

insert into @Order (id, OrderNumber, StatusId)
  values (1, 1001, 1), (2, 1002, 1), (3, 1003, 2), (4, 1004, 3), (5, 1500, 4)

declare @LineItem table (id int, OrderNumber int, LineItemNumber int, StatusId int)

insert into @LineItem (id, OrderNumber, LineItemNumber, StatusId)
  values (1, 1001, 1, 1), (2, 1001, 2, 1), (3, 1002, 1, 2), (4, 1002, 2, 1), (5, 1003, 1, 2), (6, 1004, 1, 3), (7, 1004, 2, 4), (8, 1500, 2, 3)

declare @Status table (id int, StatusDesc varchar(32))

insert into @Status(id, StatusDesc)
  values (1,'New'), (2,'Active'), (3,'Cancelled'), (4,'Complete')

-- QUERY DATA

select LI.OrderNumber, LI.LineItemNumber
  , (select S.StatusDesc from @Status S where S.id = StatusId) [StatusDesc_Order]
  , (select S.StatusDesc from @Status S where S.id = (select O.StatusId from @Order O where O.OrderNumber = LI.OrderNumber)) [StatusDesc_LineItem]
from @LineItem LI
order by LI.OrderNumber, LI.LineItemNumber

Note: If you provide your sample data in this format in future questions you make your question much easier to answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM