彩票数据库中最常用的数字

时间:2009-11-25 20:34:08

标签: database

我有一个Access 2000数据库,其中包含一个小乐透号码表。第一列是“Drawdate”,第二列是“P1”,第三列是“P2”,第四列是“P3”,第五列是“P4”,第六列是“P5”。我试图从表中找到10个最常用的数字。

一次使用5个数字将产生252个组合。从中奖号码表中我想找到10个最常用的5个数字。

3 个答案:

答案 0 :(得分:1)

我会首先取消数据,因此你有:

DrawDate, Position, Number

(1,2,3,4,5)中的位置

由于您并不关心位置,因此很容易将其从查询中排除(或完全删除列)。

便携式UNPIVOT是:

SELECT DrawDate, 1 AS Position, P1 AS [Number] FROM tbl
UNION ALL
SELECT DrawDate, 2 AS Position, P2 AS [Number] FROM tbl
UNION ALL
SELECT DrawDate, 3 AS Position, P3 AS [Number] FROM tbl
UNION ALL
SELECT DrawDate, 4 AS Position, P4 AS [Number] FROM tbl
UNION ALL
SELECT DrawDate, 5 AS Position, P5 AS [Number] FROM tbl

这实际上可以嵌入以找到最常用的数字结果,而无需重新构建数据(我甚至不打扰DrawDate和Position):

SELECT TOP 1 [Number], COUNT(*)
FROM (
    SELECT /* DrawDate, 1 AS Position, */ P1 AS [Number] FROM tbl
    UNION ALL
    SELECT /* DrawDate, 2 AS Position, */ P2 AS [Number] FROM tbl
    UNION ALL
    SELECT /* DrawDate, 3 AS Position, */ P3 AS [Number] FROM tbl
    UNION ALL
    SELECT /* DrawDate, 4 AS Position, */ P4 AS [Number] FROM tbl
    UNION ALL
    SELECT /* DrawDate, 5 AS Position, */ P5 AS [Number] FROM tbl
) AS unpivoted
GROUP BY [Number]
ORDER BY COUNT(*) DESC

如果你用一起使用的数字来定义你的意思......

答案 1 :(得分:0)

您需要计算每个号码的使用次数。

但是你不知道一个数字可能在哪一列。

因此,您可以首先计算数字1在所有列中出现的次数,然后计算数字2,依此类推。

SELECT Count(*) WHERE P1=1

答案 2 :(得分:0)

就个人而言,我会从第一张桌子开始构建第二张桌子。有点像...

INSERT INTO tblNumbers(Number,NumCount) SELECT P1, count(P1) from tblDraws group by P1 INSERT INTO tblNumbers(Number,NumCount) SELECT P2, count(P2) from tblDraws group by P2 INSERT INTO tblNumbers(Number,NumCount) SELECT P3, count(P3) from tblDraws group by P3 etc

然后在最后,执行

SELECT Number Number, SUM(NumCount) from tblNumbers group by Number