使用sql获取按小时分组的每日数据

问题描述 投票:0回答:1

我有一个mysql表,其中有两列id和出勤时间,我想每小时选择count(id)。

id 出席时间
1 1725256398
2 1725258398
3 1725264581
4 1725277777
5 1725277777
6 1725277777
7 1725298931
8 1725299156
9 1725299156

我的预期结果是:

时间 计数
11:00 2
01:00 1
05:00 3
11:00 3
I have tried following query:-

SELECT 
    CONCAT(
        DATE(FROM_UNIXTIME(attendancetime)), ' ',
        LPAD(FLOOR(HOUR(FROM_UNIXTIME(attendancetime)) / 3) * 3, 2, '0'), ':00:00'
    ) AS TIME,
    count(id) AS COUNT
FROM operationallog
GROUP BY DATE(FROM_UNIXTIME(attendancetime)), FLOOR(HOUR(FROM_UNIXTIME(attendancetime)) / 3)
ORDER BY TIME;

请提供mysql查询来实现此结果

sql mysql eloquent
1个回答
0
投票

也许你需要

SELECT DATE_FORMAT(FROM_UNIXTIME(attendancetime), '%Y-%m-%d %H:00') date_time,
       COUNT(*) amount
FROM tablename
GROUP BY 1;

© www.soinside.com 2019 - 2024. All rights reserved.