cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EBrusse
Frequent Visitor

SQL Join one column twice

Hello everyone!

 

My question is aimed at creating SQL code.

I have a table which contains 2 columns with different stockcodes for one product.

The description for this stockcode is stored in a different table.

I would like to create a dataset which displays the [Stockcode], the [DescriptionBasedOnCol1] and the [DescriptionBasedOnCol2].

This requires an SQL like:

SELECT
  a.Stockcode ,
  b.DescriptionBasedOnCol1 ,
  b.DescriptionBasedOnCol2 ,
FROM
 a
LEFT OUTER JOIN b ON a.Stockcode   = b.Stockcode

LEFT OUTER JOIN b ON a.Stockcode2 = b.Stockcode

 

Obviously this does not work. I want to know how to set this up!

Thanks in advance!

 

Erwin

1 ACCEPTED SOLUTION
Wendeley-North
Resolver I
Resolver I

Your description isn't exactly clear, but assuming you have Table 1 with columns [ProductName], [StockCode1], [StockCode2] and Table 2 with [StockCodeX], [Description]:

 

SELECT a.ProductName, a.StockCode1, b.Description AS Description1, c.Description AS Description2

FROM Table1 a

LEFT JOIN Table2 b ON a.StockCode1 = b.StockCodeX

LEFT JOIN Table2 c ON a.StockCode2 = c.StockCodeX

 

View solution in original post

2 REPLIES 2
Wendeley-North
Resolver I
Resolver I

Your description isn't exactly clear, but assuming you have Table 1 with columns [ProductName], [StockCode1], [StockCode2] and Table 2 with [StockCodeX], [Description]:

 

SELECT a.ProductName, a.StockCode1, b.Description AS Description1, c.Description AS Description2

FROM Table1 a

LEFT JOIN Table2 b ON a.StockCode1 = b.StockCodeX

LEFT JOIN Table2 c ON a.StockCode2 = c.StockCodeX

 

View solution in original post

amitchandak
Super User
Super User

@EBrusse , Create a new column in a

key = [Stockcode] & "-" & [Stockcode2]

 

new column in b

key = [Stockcode] & "-" & [Stockcode]

 

Join on these columns 

 

 

Did I answer your question? Mark my post as a solution! Appreciate your Kudos!! !!
Dashboard of My Blogs !! Connect on Linkedin

Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!