最常见的工作日查询

时间:2012-03-28 09:47:31

标签: mysql sql datetime

有这个查询:

SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
FROM users
GROUP BY hr
ORDER BY cnt DESC

DATE是DATETIME字段,上面的查询显示了日期字段中最常见的小时数。

我正在努力改进它,但不知道该怎么做,我希望按工作日和每周工作时间分解到最常见的时间。

2 个答案:

答案 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