I have a table named "loginhistory", and I need to calculate the most active hour of the month. How can i do it?
My table structure is: id, userId, date (datetime), ip.
I tried to do it in PHP but I did not succeed there either. I prefer the calculation to be done only in MySql.
I expect the output to be just the number of the most active hour.
It does not matter checking average number if needed to pick active hour.
Cause average = count / days
and since days
is the same for whole calculation (let's say 30
) the greatest count
will be active hour in list.
Just select by date range and group by hour and pick 1st one from descending sort:
SELECT
HOUR(date) AS hour,
COUNT(id) AS logins,
( COUNT(id) / DAY(LAST_DAY('2019-01-01')) ) AS logins_avg
FROM loginhistory
WHERE
date >= '2019-01-01 00:00:00' AND date < '2019-02-01 00:00:00'
GROUP BY HOUR(date)
ORDER BY logins DESC LIMIT 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.