Home Forums SQL Server 2008 T-SQL (SS2K8) Generate all possible number combinations for a provided list of numbers RE: Generate all possible number combinations for a provided list of numbers

  • Another one to try

    WITH Numbers(N) AS (

    SELECT N

    FROM ( VALUES(1),(2),(3),(4) ) Numbers(N)),

    Recur(N,Combination) AS (

    SELECT N, CAST(N AS VARCHAR(1000))

    FROM Numbers

    UNION ALL

    SELECT n.N,CAST(r.Combination + ',' + CAST(n.N AS VARCHAR(10)) AS VARCHAR(1000))

    FROM Recur r

    INNER JOIN Numbers n ON n.N > r.N)

    SELECT Combination

    FROM Recur

    ORDER BY LEN(Combination),Combination;

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537