![](/img/trans.png)
[英]calculate the average number of hours per day that the employee spent on the workplace for the previous month
[英]Calculate the average number of hours in table per month
我有一個名為“ loginhistory”的表,我需要計算一個月中最活躍的小時。 我該怎么做?
我的表結構是:id,userId,日期(datetime),ip。
我試圖用PHP做到這一點,但我也沒有成功。 我希望僅在MySql中進行計算。
我希望輸出只是最活躍的小時數。
如果需要選擇活動時間,則檢查平均數無關緊要。
原因average = count / days
,由於整個計算的days
相同(假設為30
),因此最大的count
將是列表中的活動小時count
。
只需按日期范圍選擇並按小時分組,然后從降序中選擇第一個即可:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.