I'll be honest that I don't really understand the way you've expressed your requirement, however I believe this solution matches the result set you listed.
-- Solution 3: List combinations up to 3
;WITH UNIQUEnTuples (n, Class, Product, Tuples, Product2) AS (
SELECT 1, Class, Product
,CAST(Product AS VARCHAR(MAX)), NULL
FROM @ProductClass
UNION ALL
SELECT 1 + n.n, t.Class, t.Product
,CAST(t.Product AS VARCHAR(MAX)) + '+' + n.Tuples
,n.Product
FROM @ProductClass t
JOIN UNIQUEnTuples n ON CAST(t.Product AS VARCHAR(MAX)) < n.Tuples AND
n.Class <> t.Class
WHERE CHARINDEX(CAST(t.Product AS VARCHAR(MAX)), n.Tuples) = 0 AND n < 3
)
SELECT [Group ID]=CASE n WHEN 3 THEN 1 ELSE 2 END, [Item Group ID]=[Group ID], Product=item
FROM (
SELECT [Group ID]=ROW_NUMBER() OVER (PARTITION BY n ORDER BY Product2, Product)
,[Product Group]=Tuples, n
FROM UNIQUEnTuples
WHERE n IN (2,3)) a
CROSS APPLY dbo.DelimitedSplit8K([Product Group], '+')
ORDER BY CASE n WHEN 3 THEN 1 ELSE 2 END
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St