1

我有一张客户访问企业的表格,其中包含日期列和客户信息列。

+------+---------------------+------+
| id   | checkin             | name | ...
+------+---------------------+------+
|    1 | 2010-01-01 00:12:00 | Joe  | 
|    2 | 2010-01-01 00:18:00 | John |
|    3 | 2010-01-01 00:22:00 | Jane |
...

我想按星期几获取统计信息,即按星期几列出的平均、最小、最大访问量。就像是:

+---------+---------+---------+
| day     | average | minimum | ...
+---------+---------+---------+
| Monday  | 45      | 30      | 
| Tuesday | 60      | 35      |
...

我相信我需要使用子查询或连接才能做到这一点,但无法弄清楚如何。

此查询将汇总每天的访问次数

SELECT dayname(checkin) as day, count(*) as total
FROM customer_log
group by date(checkin)

然后在上面的结果上我需要运行类似的东西

SELECT sum(total), max(total), min(total)
from {above table}
group by day.

谢谢!

4

1 回答 1

1

试试这个:

SELECT dayname(checkinDate) day,
  sum(visitsPerDay) totalVisits,
  avg(visitsPerDay) avgVisits,
  max(visitsPerDay) maxVisits,
  min(visitsPerDay) minVisits
FROM (
  SELECT date(checkin) checkinDate, count(*) visitsPerDay FROM Customer_log
  GROUP BY checkinDate
) AS visitsPerDaySub
GROUP BY dayofweek(checkinDate)

于 2012-11-14T19:54:21.300 回答