In MSSQL, I have a table (ProductRecipe) that contains up to 5 different components products. I then have a dataset that contains the individual component products and their costs.
What I'm trying to do is find all the different permutations/combinations that will satisfy my recipe.
CREATE TABLE #ProductRecipe (ProductRecipeID INT, Component1 INT, Component2 INT, Component3 INT, Component4 INT, Component5 INT)
CREATE TABLE #ComponentPricing (RowID INT, PricingID INT, ProductID INT, ProductDescription VARCHAR(50), Cost DECIMAL(18,6))
INSERT INTO #ProductRecipe (ProductRecipeID, Component1, Component2) VALUES (21, 130, 468)
INSERT INTO #ComponentPricing (RowID, PricingID, ProductID, ProductDescription, Cost)
VALUES (1, 314023, 130, 'ULS2', 1.783800)
, (2, 313616, 130, 'ULS2', 1.783800)
, (3, 313071, 130, 'ULS2', 1.794000)
, (4, 312865, 130, 'ULS2', 1.789500)
, (5, 316323, 468, 'B100', 1.550500)
SELECT * FROM #ProductRecipe
SELECT * FROM #ComponentPricing
DROP TABLE #ProductRecipe
DROP TABLE #ComponentPricing
The result I'm trying to achieve is that I end up with 4 different variations of the recipe since the first 4 records (for ProductID 130) can be blended with the last record (ProductID 468). Only these two products can be blended because those are the two component products defined in my ProductRecipe table.
Desired Result: Row 1+5 go together, Row 2+5 go together, Row 3+5 go together, Row 4+5 go together; Returning the PricingID column.
ProductRecipeID Component1 Component2 Component3 Component4 Component5
21 314023 316323
21 313616 316323
21 313071 316323
21 312865 316323