美文网首页
SQLServer排列组合,六个字母中取三个排列组合

SQLServer排列组合,六个字母中取三个排列组合

作者: 故事的开头是个码农 | 来源:发表于2019-03-25 14:09 被阅读0次

    六个字母abcdef中取三个任何组合,例如abc,acb,efc求最后的结果

    
    
    DECLARE @T TABLE(
    COL1 VARCHAR(100)
    )
    INSERT INTO @T
    SELECT 'A' UNION ALL
    SELECT 'B' UNION ALL
    SELECT 'C' UNION ALL
    SELECT 'D' UNION ALL
    SELECT 'E' UNION ALL
    SELECT 'F'
    SELECT * FROM (SELECT COL1 FROM @T
    UNION ALL
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  @T T1
    FULL  JOIN @T T2 ON  T1.COL1<>T2.COL1
    UNION ALL
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  @T T1
    FULL  JOIN (
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  @T T1
    FULL  JOIN @T T2 ON  T1.COL1<>T2.COL1
    ) T2 ON  T1.COL1<>T2.COL1
    UNION ALL
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  @T T1
    FULL  JOIN (
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  (
    SELECT T1.COL1+T2.COL1 'COL1'  FROM  @T T1
    FULL  JOIN @T T2 ON  T1.COL1<>T2.COL1
    ) T1
    FULL  JOIN @T T2 ON  T1.COL1<>T2.COL1
    ) T2 ON  T1.COL1<>T2.COL1) A where LEN(COL1)=3
    

    相关文章

      网友评论

          本文标题:SQLServer排列组合,六个字母中取三个排列组合

          本文链接:https://www.haomeiwen.com/subject/obskuqtx.html