• You are looking for someone to spoon-feed you a solution, without trying to examine exactly what UNIQUEnTuples does.

    -- Solution 4: Generate all combinations

    ;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

    )

    SELECT *

    FROM UNIQUEnTuples

    Start with the above. I have eliminated the constraint on how many products are combined.

    You'll need to decompose the prior code I provided to you to figure out how to split the tuples and then combine them to suit your needs.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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