我有一个这样的数据库:
custNum date purchase dayOfWeek
333 2001-01-01 23.23 1
333 2001-03-04 34.56 5
345 2008-02-02 22.55 3
345 2008-04-05 12.35 6
... ... ... ...
我正在尝试为每个客户获取dayOfWeek列的模式(最常出现的值)。基本上,这将是每个客户购买最多的一周中的一天。像:
custNum max(count(dayofweek(date)))
333 5
345 3
356 2
388 7
... ...
任何帮助都会非常感谢。
答案 0 :(得分:3)
select custNum, dayOfWeek
from tableName t
group by custNum, dayOfWeek
having dayOfWeek = (
select dayOfWeek
from tableName
where custNum = t.custNum
group by dayOfWeek
order by count(*) desc, dayOfWeek
limit 1
)