Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

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

 

amitchandak
Super User
Super User

@Anonymous , Create a new column in a

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

 

new column in b

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

 

Join on these columns 

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.