这一定是常见的事情,但我被困住了。
我有一些可以简化的数据:
id user unixtime
-----------------------
1 dave 1335312057
2 dave 1335312058
3 steve 1335312128
等
到目前为止,我只需要按天聚合,所以我一直在使用:
SELECT
UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(unixtime))) AS time,
count(c.user) AS count
FROM core c
GROUP BY DATE(FROM_UNIXTIME(unixtime))
我在DATE和HOUR尝试使用CONCAT,但是不能让它按预期运行 - 任何想法?
答案 0 :(得分:5)
SELECT
DATE(FROM_UNIXTIME(unixtime)) as date,
HOUR(FROM_UNIXTIME(unixtime)) AS hour,
count(c.user) AS count
FROM core c
GROUP BY 1,2
如果您希望将小时作为unix时间戳,请将此查询包装起来以获取它:
SELECT UNIX_TIMESTAMP(DATE_ADD(the_date, INTERVAL the_hour HOUR)), the_count
from (select
DATE(FROM_UNIXTIME(unixtime)) as the_date,
HOUR(FROM_UNIXTIME(unixtime)) as the_hour,
count(c.user) AS the_count
FROM core c
GROUP BY 1,2
) x
注意:在列名上使用the_
前缀以避免保留字出现问题
答案 1 :(得分:1)
尝试使用
SELECT
UNIX_TIMESTAMP(DATE_ADD(DATE(FROM_UNIXTIME(unixtime)), INTERVAL HOUR(FROM_UNIXTIME(unixtime)) HOUR)) AS TIME,
COUNT(c.user) AS COUNT
FROM core c
GROUP BY DATE(FROM_UNIXTIME(unixtime)), HOUR(FROM_UNIXTIME(unixtime));
希望这会有所帮助......