sql table:总计超过10秒的间隔

时间:2013-08-05 22:29:49

标签: mysql sql

我正在为我的统计问题寻找解决方案:

我有一个包含一个日期列的表。现在我要做的是计算特定时间间隔为10秒的行数:

SELECT count(id),... AS interval WHERE ... GROUP BY interval

有人知道如何执行此操作吗?

谢谢, 马库斯

1 个答案:

答案 0 :(得分:1)

SELECT
  CONCATENATE(
   SUBSTR(
    DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
    , 1, -1)
   , 0) AS time_bin,
  COUNT(*)
FROM yourtable
WHERE ...
GROUP BY CONCATENATE(
   SUBSTR(
    DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
    , 1, -1)
   , 0)

(使用str_to_date()转换回时间戳) 或...

SELECT FROM_UNIXTIME(
    10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
) AS time_bin,
COUNT(*)
FROM yourtable
WHERE ...
GROUP BY FROM_UNIXTIME(
    10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
)