[整理]枚举双色球的红球所有排列组合的SQL

由数学上的排列组合可以计算出来,由于双色球不排序,只组合,所以33红球和16篮球全选的话有   17721088种可能。

--电脑很慢,用了3:46"
;WITH CTE AS
(
SELECT TOP 33 ID=ROW_NUMBER() OVER (ORDER BY GETDATE()) FROM syscolumns
)
,CTE2 AS
(
SELECT ID,CAST(ID AS VARCHAR(8000)) AS COP,LVL=1 FROM CTE
UNION ALL
SELECT CTE.ID,COP=COP+','+RTRIM(CTE.ID),LVL+1 FROM CTE,CTE2 WHERE CTE.ID<CTE2.ID AND LVL<6
)
SELECT ROW_NUMBER() OVER (ORDER BY GETDATE()),COP FROM CTE2 WHERE LVL=6
/*
                     COP
-------------------- ---------------------------------------------------------
1                    33,32,31,30,29,1
2                    33,32,31,30,29,2
3                    33,32,31,30,29,3
4                    33,32,31,30,29,4
5                    33,32,31,30,29,5
6                    33,32,31,30,29,6
..........
1107564              7,6,5,4,2,1
1107565              7,6,5,3,2,1
1107566              7,6,4,3,2,1
1107567              7,5,4,3,2,1
1107568              6,5,4,3,2,1


(1107568 row(s) affected)
*/


【SQLServer】

SELECT TOP 33 id=identity(int,1,1) INTO #1 FROM sysobjects

SELECT A.id ,B.id,C.id,D.id,E.id,F.id 
FROM #1 A,#1 B,#1 C,#1 D,#1 E,#1 F
WHERE A.ID<B.ID AND A.ID<C.ID AND A.ID<D.ID AND A.ID<E.ID AND A.ID<F.ID
AND B.ID<C.ID AND B.ID<D.ID AND B.ID<E.ID AND B.ID<F.ID
AND C.ID<D.ID AND C.ID<E.ID AND C.ID<F.ID
AND D.ID<E.ID AND D.ID<F.ID
AND E.ID<F.ID
ORDER BY A.id,B.id,C.id,D.id,E.id,F.id

--result : 1107568 SQL Server 2005 46's

-- 创建号码表(1-33)tb_rednum

SELECT TOP 33 id=identity(int,1,1) INTO dbo.tb_rednum FROM sysobjects

-- 创建结果表 (所有组合) tb_allred

SELECT A.id num01 ,B.id num02,C.id num03,D.id num04,E.id num05,F.id num06 into dbo.tb_allred

FROM tb_rednum A,tb_rednum B,tb_rednum C,tb_rednum D,tb_rednum E,tb_rednum F
WHERE A.ID<B.ID AND A.ID<C.ID AND A.ID<D.ID AND A.ID<E.ID AND A.ID<F.ID
AND B.ID<C.ID AND B.ID<D.ID AND B.ID<E.ID AND B.ID<F.ID
AND C.ID<D.ID AND C.ID<E.ID AND C.ID<F.ID
AND D.ID<E.ID AND D.ID<F.ID
AND E.ID<F.ID

引自:http://blog.csdn.net/flyloafer/article/details/5221137     徐哲


【Oracle】

With X As (Select Rownum Id From Dict Where Rownum<33)
Select A.Id R1,B.Id R2,C.Id R3,D.Id R4,E.Id R5,F.Id R6
From X A,X B,X C,X D,X E,X F
Where A.Id<B.Id And A.Id<C.Id And A.Id<D.Id And A.Id<E.Id And A.Id<F.Id
And B.Id<C.Id And B.Id<D.Id And B.Id<E.Id And B.Id<F.Id
And C.Id<D.Id And C.Id<E.Id And C.Id<F.Id
And D.Id<E.Id And D.Id<F.Id
And E.Id<F.Id
Order By A.Id,B.Id,C.Id,D.Id,E.Id,F.Id;


 双色球彩票33选6算法:M个数N个为一组,所有无重复的排列组合

 应朋友请求做的双色球彩票33选6算法:M个数N个为一组,所有无重复的排列组合。测试基本没问题,欢迎彩票迷们使用!

   private void button1_Click(object sender, EventArgs e)
        {
            int nCnt = 0;
            List<int> nNumList = new List<int>();
            for (int i = 0; i < cblNumList.Items.Count; i++)
            {
                if (cblNumList.GetItemChecked(i))
                {
                    nNumList.Add(Convert.ToInt32(cblNumList.Items[i].ToString()));
                }
            }
            System.Text.StringBuilder sbResult = new StringBuilder();

            int nLast = nNumList.Count;

            for (int i1 = 0; i1 < nLast-5; i1++)
            {
                for (int i2 = 0; i2 < nLast - 4; i2++)
                {
                    if (i2 == i1) continue;
                    for (int i3 = 0; i3 < nLast - 3; i3++)
                    {
                        if (i3 == i2 || i3 == i1) continue;
                        for (int i4 = 0; i4 < nLast - 2; i4++)
                        {
                            if (i4 == i3 || i4 == i2 || i4 == i1) continue;
                            for (int i5 = 0; i5 < nLast - 1; i5++)
                            {
                                if (i5 == i4 || i5 == i3 || i5 == i2 || i5 == i1) continue;
                                for (int i6 = 0; i6 < nLast; i6++)
                                {
                                    if (i6 == i5 || i6 == i4 || i6 == i3 || i6 == i2 || i6 == i1) continue;
                                    if (nNumList[i1] > nNumList[i2] || nNumList[i2] > nNumList[i3] || nNumList[i3] > nNumList[i4] || nNumList[i4] > nNumList[i5] || nNumList[i5] > nNumList[i6]) continue;
                                    sbResult.Append("\r\n" + nNumList[i1].ToString() + " " + nNumList[i2].ToString() + " " + nNumList[i3].ToString() + " " + nNumList[i4].ToString() + " " + nNumList[i5].ToString() + " " + nNumList[i6].ToString());
                                    nCnt += 1;
                                }
                            }
                        }
                    }
                }
            }
            tbResult.Text = sbResult.ToString();
            //lblMessage.Text = nLast + " 个随机数字;共有 " + nCnt + " 条记录";

        }


你可能感兴趣的:(oracle,sql,sql,c,server,sqlserver)