Reputation: 63
I have one product table with following info.
ID ProductId Name OtherData
1 0 A data1 2 0 B data2 3 1 A1 NULL 4 1 A2 NULL
I need all data with detail ProductId is relationship with ID column.
I need result like below
ID ProductId Name OtherData
1 0 A data1 2 0 B data2 3 1 A1 data1 4 1 A2 data1
What kind of join or query I should use?
Upvotes: 0
Views: 58
Reputation:
SELECT s.ID, s.ProductId, s.Name,
OtherData = COALESCE(s.OtherData, r.OtherData)
FROM dbo.Products AS s
LEFT OUTER JOIN dbo.Products AS r
ON s.ProductId = r.ID;
Upvotes: 2