powerset(all combinations) of a resultset in T-SQL

筅森魡賤 提交于 2019-12-20 07:49:13

问题


I need a t-sql code to get powerset of a resultset.

example input :

ColumnName
1
2
3

Example Output(one columns as nvarchar) :
1
2
3
1,2
1,3
2,3
1,2,3

Output set may contain duplicate values such as (1,3 vs 3,1).


回答1:


You can use this one and get full powerset.
Enjoy it.

--EXEC PowerSet 'A,B,C,D'
Create PROCEDURE PowerSet(@Members NVARCHAR(64))
As
Begin
    Declare @SubSet NVARCHAR(Max),
            @SubSetCount int,
            @Counter1 int,
            @Counter2 int,
            @ID int
    Create table #Members
    (
        ID int IDENTITY(1,1) PRIMARY KEY,
        Member NVARCHAR(50)
    )
    Create table #PowerSets
    (
        SubSet NVARCHAR(50)
    )
    -- Start Convert @Members To Table #Members
    Set @Members=@Members+','
    While @Members<>''
    Begin
        Insert Into #Members(Member) 
        Values(Left(@Members,CHARINDEX(',',@Members)-1))
        Set @Members=RIGHT(@Members,LEN(@Members)-CHARINDEX(',',@Members))
    End
    -- End Convert @Members To Table #Members
    -- Start Calculat PowerSet
    Set @SubSetCount=POWER(2,(Select Count(*) From #Members))
    Set @Counter1=0
    while @Counter1<@SubSetCount
    Begin
        Set @Counter2=@Counter1
        Set @ID=1
        Set @SubSet='{'
        While @Counter2>0
        Begin
            If CONVERT(char(1), @Counter2 % 2)=1
                 Set @SubSet=@SubSet+iif(@SubSet<>'{',',','')+(Select Member From #Members Where ID=@ID)
            Set @Counter2 = CONVERT(int, (@Counter2 / 2))
            Set @ID=@ID+1
        End
        Set @Counter1=@Counter1+1
        Insert into #PowerSets(SubSet) values(@SubSet+'}')
    End
    -- End Calculat PowerSet
    Select *
    From #PowerSets
    Order By Len(SubSet),SubSet
    Drop Table #PowerSets
    Drop Table #Members 
End


来源:https://stackoverflow.com/questions/36859361/powersetall-combinations-of-a-resultset-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!