2

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

我有一张表,里面有一个日期列。现在我要做的是在 10 秒的特定时间间隔内计算行数:

SELECT count(id), ... AS 间隔 WHERE ... GROUP BY 间隔

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

谢谢,马库斯

4

1 回答 1

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)
)
于 2013-08-05T22:59:22.143 回答