有这个查询:
SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY hr
ORDER BY cnt DESC
DATE是DATETIME字段,上面的查询显示了日期字段中最常见的小时数。
我正在努力改进它,但不知道该怎么做,我希望按工作日和每周工作时间分解到最常见的时间。
答案 0 :(得分:1)
如果下面有效,请告诉我:
SELECT dayname(DATE) as week_day,HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY week_day, hr ORDER BY cnt DESC
答案 1 :(得分:1)
尝试此查询 -
SELECT
DAYOFWEEK(DATE) AS wd,
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC
SELECT
DATE_FORMAT(DATE, '%a') AS wd, -- or DATE_FORMAT(DATE, '%W')
HOUR(DATE) AS hr,
COUNT(*) AS cnt
FROM
users
GROUP BY
wd, hr
ORDER BY
cnt DESC