Do you want each price level as a separate column in the resultset?
If so, then you will can JOIN the ProductProgram table repeatedly for each required level. Something like:
Code:
SELECT Name, Description,
PP1.Price as Level1Price,
PP2.Price as Level2Price,
PP3.Price as Level3Price,
PP4.Price as Level4Price,
PP5.Price as Level5Price
FROM Product
INNER JOIN ProductProgram PP1
ON Product.ProductID = PP1.ProductID AND PP1.Level = 1
INNER JOIN ProductProgram PP2
ON Product.ProductID = PP2.ProductID AND PP2.Level = 2
INNER JOIN ProductProgram PP3
ON Product.ProductID = PP3.ProductID AND PP3.Level = 3
INNER JOIN ProductProgram PP4
ON Product.ProductID = PP4.ProductID AND PP4.Level = 4
INNER JOIN ProductProgram PP5
ON Product.ProductID = PP5.ProductID AND PP5.Level = 5
This could also be done via correlated subqueries:
Code:
SELECT Name, Description,
(SELECT ProductPrograms.Price FROM ProductPrograms
WHERE ProductPrograms.ProductID = Products.ProductID
AND Level = 1) as Level1Price,
... (similarly for the other 4 level values)
FROM Product
...
It's not clear to me which is a better solution. Indeed, each may result in the same execution plan.
However, note also that if there are missing level values things become a bit more complex with the INNER JOIN solution. An OUTER JOIN will be required in that case, and that is left as an exercise to the reader...
Jeff Mason
Custom Apps, Inc.
[email protected]