Forum Discussion
Anonymous
5 years agoNot 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 d...
- 5 years ago
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
5 years agoSuper User
Anonymous , Create a new column in a
key = [Stockcode] & "-" & [Stockcode2]
new column in b
key = [Stockcode] & "-" & [Stockcode]
Join on these columns