I'm having trouble figuring out how I can best create the desired end result of this SQL query without nesting SELECT statements which just seems awful. So awful. I feel like it may have something to do with a PIVOT, but I'm trying to find the most efficient way to do this.
I have dumbed down these tables as an example. I have 2 tables: PRODUCTS and PRODUCT_PRICING. The PRODUCT_PRICING table will have multiple instances of each product for each week that the price has changed.
PRODUCTS
PKey | ProductName
------------------------
10 Hammer
24 Screw Driver
31 Nail
48 Wrench
PRODUCT_PRICING
PKey | ProductPKey | ProductPrice | WeekNumber
----------------------------------------------------------
1 10 10.00 20
2 10 9.50 21
3 24 6.00 21
4 31 0.01 15
5 31 0.02 21
6 48 7.50 21
What I'm trying to do is pass in a week number (21) and get all of the results in a structure like this:
WeekNumber | HammerPKey | HammerPrice | ScrewDriverPKey | ScrewDriverPrice | NailPKey | NailPrice | WrenchPKey | WrenchPrice
21 10 9.50 24 6.00 31 0.02 48 7.50