[英]SQL : I search the good query with select max() and select count()
I have a table named arrived (this table is simple, and it just saves the users that arrive at school at such day and at such hour) with the following data : 我有一个名为到达的表(此表很简单,它只保存在当天和该小时到达学校的用户),其中包含以下数据:
id name day hour
1 Alice Monday 11
2 Alice Monday 13
3 Alice Tuesday 11
4 Céline Wednesday 14
5 Céline Wednesday 13
6 Céline Thursday 14
7 Maud Friday 15
8 Maud Saturday 15
9 Maud Saturday 16
Now, I search the good query that find for each user : the most frequent day and the most frequent hour, that is say, the result of the query must return this lines : 现在,我搜索为每个用户找到的良好查询:最频繁的一天和最频繁的小时,也就是说,查询结果必须返回以下行:
Alice Monday 11
Céline Wednesday 14
Maud Saturday 15
=> because : =>因为:
My query is below, but it doesn't give me the good result : 我的查询在下面,但没有给我好的结果:
SELECT NAME,
day,
Max(count_hour)
FROM (SELECT NAME,
day,
Count(hour) AS count_hour
FROM arrived
GROUP BY NAME,
day) AS alias_table
GROUP BY NAME
Thank you, cordially. 衷心谢谢。
Try the greatest(day, hour)
when selecting columns. 选择列时尝试greatest(day, hour)
。 You can also try greatest(max(day), max(hour))
您还可以尝试greatest(max(day), max(hour))
Cheers! 干杯!
Not sure is this the right way to do it but should work! 不确定这是否是正确的方法,但应该可以!
SELECT *
FROM arrived A
WHERE hour = (SELECT hour
FROM arrived B
WHERE a.NAME = b.NAME
GROUP BY b.hour,
b.NAME
ORDER BY Count(b.hour) DESC Limit 1)
AND day = (SELECT day
FROM arrived B
WHERE a.NAME = b.NAME
GROUP BY b.day,
b.NAME
ORDER BY Count(b.day) DESC Limit 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.