使用MySQL按分钟分组和计数

时间:2012-02-16 01:44:58

标签: mysql count

大家好,我真的很想从一个大型时间戳数据库中获取每天每分钟的计数,我希望能够根据数据构建图表。

所以一天有1440分钟,我怎样才能得到每分钟的计数?

我可以在一小时内得到每分钟的计数,我很难知道如何扩展以获得当天每分钟的计数。

我现在有这个代码,

 SELECT FROM_UNIXTIME(
         CEILING(UNIX_TIMESTAMP(`timestamp`)/900)*900
                    ) AS timeslice
     , COUNT(*) AS mycount
  FROM visitor
 WHERE `timestamp` >= '20012-01-01'
   AND `timestamp`  < '20012-02-14'
GROUP BY timeslice 

我希望这有意义并感谢您的帮助!

有任何问题请问我

4 个答案:

答案 0 :(得分:3)

根据记录的数量,您可以尝试使用以下查询(检查其执行时间)。

select d.everyminute, 
      (select count(*) from visitor 
              where `timestamp` between d.everyminute and 
                                        d.everyminute + interval 59 second) cnt 
      from 
(
 select @rownum:=@rownum+1, 
       date('2012-02-15') + interval (@rownum-1) minute everyminute from

(select 0 union all select 1 union all select 2) t,

(select 0 union all select 1 union all select 2 union all select 3 union all 
 select 4 union all select 5 union all select 6 union all select 7 union all 
 select 8 union all select 9) t1,

(select 0 union all select 1 union all select 2 union all select 3 union all 
 select 4 union all select 5 union all select 6 union all select 7 union all 
 select 8 union all select 9) t2,

(select 0 union all select 1 union all select 2 union all select 3 union all 
 select 4 union all select 5 union all select 6 union all select 7 union all 
 select 8 union all select 9) t3,

(select @rownum:=0) r where @rownum < 24*60
) d

结果是(我将一些时间戳放入测试数据库)

2012-02-15 00:00:00 0
2012-02-15 00:01:00 0
2012-02-15 00:02:00 2
2012-02-15 00:03:00 1
2012-02-15 00:04:00 0
2012-02-15 00:05:00 1
2012-02-15 00:06:00 0
2012-02-15 00:07:00 0
2012-02-15 00:08:00 0
2012-02-15 00:09:00 0
2012-02-15 00:10:00 0

,直到2012-02-15 23:59:00

现有记录的时间戳为2012-02-15 00:02:002012-02-15 00:02:002012-02-15 00:03:002012-02-15 00:05:00

ps:考虑到夏季/冬季时间跳跃,您可以将where @rownum < 24*60重写为having everyminute < (date('2012-02-15') + interval 1 day - interval 1 minute)

正如您所看到的,即使在这一分钟内db中没有记录,它也会计入每分钟。

答案 1 :(得分:2)

最简单的方法是创建一个已经有几分钟的实用程序表。如果你很聪明,你也可以把时间和经络放在一起。

您可以通过搜索数据仓库时间维度找到更多信息。

答案 2 :(得分:2)

看起来你不希望它按日/月/年分组,只需几分钟:

select minute, sum(if(adate is null, 0, 1)) DateAmount from (
  select @num := @num + 1 as minute from
    (select 1  union all select 2  union all select 3  union all
     select 4  union all select 5  union all select 6  union all
     select 7  union all select 8  union all select 9  union all
     select 10 union all select 11 union all select 12) as t1,
    (select 1  union all select 2  union all select 3  union all
     select 4  union all select 5  union all select 6  union all
     select 7  union all select 8  union all select 9  union all
     select 10 union all select 11 union all select 12) as t2,
    (select 1  union all select 2  union all select 3  union all
     select 4  union all select 5  union all select 6  union all
     select 7  union all select 8  union all select 9  union all
     select 10) as t3,
    (select @num := - 1) as t4
) as MinuteInDay
left join visitors v
on minute(v.aDate) = MinuteInDay.minute
group by minute

对于这组数据:

+---------------------+
|        aDate        |
+---------------------+
| 2012-01-01 00:00:00 |
| 2012-01-01 00:01:00 |
| 2012-01-01 00:02:00 |
| 2012-01-01 00:02:00 |
| 2012-01-02 00:02:00 |
| 2012-01-03 00:03:00 |
+---------------------+

这将导致:

+--------+------------+
| MINUTE | DATEAMOUNT |
+--------+------------+
| 0      | 1          |
| 1      | 1          |
| 2      | 3          |
| 3      | 1          |
| 4      | 0          |
| 5      | 0          |
| ...    | ...        |
| 1439   | 0          |
+--------+------------+

希望这有帮助。

PS:如果你不需要0中的分钟而只是省略它们会更容易!

答案 3 :(得分:0)

我会做这样的事情:

SELECT MOD(FLOOR(UNIX_TIMESTAMP( {时间戳{1}} {时间戳{1}} {时间戳{1}}

它将计算自unix时代以来的1440分钟批次,忽略夏令时等。