SQL获取每分钟的数量

时间:2015-04-14 22:30:47

标签: mysql sql

我有一个像:

这样的数据集
id       name      updated_at
1        test1     2014-06-30 09:00:00
1        test2     2014-06-30 09:01:10
1        test3     2014-06-30 09:01:23
1        test4     2014-06-30 09:01:43
1        test5     2014-06-30 09:02:02
1        test6     2014-06-30 09:02:34
1        test7     2014-06-30 09:03:22
1        test8     2014-06-30 09:03:28

我需要计算最后十分钟的分钟数。所以它应该总是返回十个数字,即最后更新的行数。关于如何有效地做到这一点的任何想法?

2 个答案:

答案 0 :(得分:0)

最近10个结果

http://sqlfiddle.com/#!9/3d586/22

--get the minute component of the update time
select minute(updated_at) as Sec
--count the number of records which have this minute
, count(1) as Cnt 
from myTable
--use group by to ensure we return 1 row per minute
group by minute(updated_at)
--list from most recent working backwards
order by minute(updated_at) desc
--return up to 10 results
limit 10

过去10分钟的结果

http://sqlfiddle.com/#!9/3d586/26

--get the minute component of the update time
select minute(y.d) as Min
--count the number of records which have this minute
--use m.id instead of 1 or * to ensure where there's no result from myTable
--we don't count any rows
, count(m.id) as Cnt 
from
(
    --get the current date's minute, offset by a given amount
    select date_add(now(), interval -x.a minute) d
    from
    (
       --the list of given amounts by which to offset the above date
       select 0 a
       union select 1
       union select 2
       union select 3
       union select 4
       union select 5
       union select 6
       union select 7
       union select 8
       union select 9
    ) x
) y
--left join to ensure the above list drives which results we get, 
--regardless of whether there are matching entries in myTable
left outer join myTable m
--join on the minute of each date 
on minute(m.updated_at) = minute(y.d)
--use group by to ensure we return 1 row per minute
group by minute(y.d)
--list from most recent working backwards
order by minute(y.d) desc

答案 1 :(得分:0)

由于您需要插值,因此它会变得更加复杂。最简单的方法就是创建一个派生表,每分钟从最后十分钟开始,然后离开连接到那一行。这是一种方法:

   select minute(now() - interval m minute) 'minutes ago', count(case when t.updated_at is not null then t.updated_at end)
from
  (select minute(now()) m
     union 
   select minute(now() - interval 1 minute) m
     union 
   select minute(now() - interval 2 minute) m
     union 
   select minute(now() - interval 3 minute) m
     union 
   select minute(now() - interval 4 minute) m
     union 
   select minute(now() - interval 5 minute) m
     union 
   select minute(now() - interval 6 minute) m
     union 
   select minute(now() - interval 7 minute) m
     union 
   select minute(now() - interval 8 minute) m
     union 
   select minute(now() - interval 9 minute) m
     union 
   select minute(now() - interval 10 minute) m
  ) q
  left join myTable t
    on q.m = minute(t.updated_at) 
      and t.updated_at >= now() - interval 10 minute
  group by m
  order by m desc

这里有一个小提琴,但你必须改变架构(只是在某处添加一个空格)并重建它以获得准确的结果:

http://sqlfiddle.com/#!9/f3320/1