4

I have two tables, one is a product transforming history table and the other is the products table:

dbo.Product_Change_History

Date  FK_ProductID FK_FinalProductID ChangedBy
20160709    1               3           John

dbo.Product_Detail

PK_ProductID ProductName 
     1        Red Bike
     3        Green Bike

I would like a table like:

 Date      ProductName FinalProductName
20160709   Red Bike     Green Bike

How do I join the two tables to achieve this? edit: This is in TSQL

4 Answers 4

6

You'd have to join twice, which means you to alias at least one of the joins:

SELECT Date, foo.ProductName, bar.FinalProductName
FROM Product_Change_history
LEFT JOIN Product_Detail AS foo ON fk_productid = foo.pk_productid
LEFT JOIN Product_Detail AS bar ON fk_finalproductid = bar.pk_productid
3
  • Hi Marc, thanks for your help - can you please use the same aliases as I have in my questions - you have used pk_finalproductid and I would not have this for example
    – WillD
    Sep 8, 2016 at 18:31
  • @WillD : you didn't have any aliases in your question. You didn't even have any query at all... Sep 8, 2016 at 18:35
  • I really appreciate the answer but to be fair you have turned this into a negative comment for no reason - your answer does not use the same column headers as my question or the same table names. I cannot work out where you have got pk_finalproductid from as I do not have this equivalent.
    – WillD
    Sep 8, 2016 at 18:41
1

I'm not sure about LEFT joins VS INNER. I would think this would work well.

SELECT
 link.Date,
 old.ProductName,
 new.ProductName as FinalProductName
FROM
 dbo.Product_Detail as old
 INNER JOIN dbo.Product_Change_History as link
  ON old.PK_ProductID = link.FK_ProductID
 INNER JOIN dbo.Product_Detail as new
  ON new.PK_ProductID = link.FK_FinalProductID
0
declare @ProdChange table(ProductID int, FK_ProductID INT, FK_FinalProductID INT, ChangedBy Varchar(100))

declare @Prod table(ProductID int, ProductName Varchar(100))

insert into @Prod Values(1,'Red Bike')
insert into @Prod Values(3,'Green Bike')

insert into @ProdChange Values(1,1,3,'John')
select * from @Prod
select * from @ProdChange

SELECT 
 old.ProductName,
 new.ProductName as FinalProductName
FROM
 @Prod as old
 INNER JOIN @ProdChange as link
  ON old.ProductID = link.FK_ProductID
 INNER JOIN @Prod as new
  ON new.ProductID = link.FK_FinalProductID
0

Thank you for the responses, with the help Chad Zinks answer I came to the correct solution:

select
PRDHIST.Date,
OLDPROD.ProductName,
NEWPROD.ProductName
from dbo.Product_ChangeHistory PRDHIST
inner join dbo.Product_Detail OLDPROD
on OLDPROD.PK_ProductID = PRDHIST.FK_ProductID
inner join dbo.Product_Detail NEWPROD
on NEWPROD.PK_ProductID = PRDHIST.FK_FinalProductID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.