我需要一些帮助来编写一个每天抓取模式(最常出现的数字)的MySQL查询
数据集看起来像这样
datetime number 2010-01-01 00:35:00 500 2010-01-01 01:15:10 500 2010-01-02 01:25:10 1500 2010-01-02 01:35:10 50 2010-01-03 12:35:50 100 2010-01-05 05:25:10 2500
(等)
答案 0 :(得分:1)
以下内容应该为您提供每日模式:
SELECT x.* FROM
(
SELECT datetime, number, COUNT(*) AS Ct
FROM table
GROUP BY datetime, number
) x
INNER JOIN
(
SELECT datetime, MAX(Ct) AS MaxCt
FROM (
SELECT datetime, number, COUNT(*) AS Ct
FROM table
GROUP BY datetime, number
) y
GROUP BY datetime
) z
ON z.MaxCt = x.Ct
AND z.datetime = x.datetime
n.b。如果您有多个具有相同模态频率的数字,它不会做任何想象 - 您将获得该日期的多行。
答案 1 :(得分:0)
您可以使用GROUP BY和LIMIT在给定的一天内完成此操作:
SELECT number, count(number) AS qty
FROM table
GROUP BY number
ORDER BY qty DESC
LIMIT 1;