用于获取用户最频繁的时间和/或时间段的 SQL 语句
我是 SQL 日期时间新手。
考虑到我在 MySQL 中有带有日期时间数据的记录(如下所示), 获取特定用户(即约翰)的这些记录最频繁出现的小时或范围/期间的最佳方法是什么?
ID | Datetime | User | Activity
0 2010-03-29 13:15:56 john visit
1 2010-03-29 13:13:14 ariel visit
2 2010-03-29 13:09:13 john visit
3 2010-03-29 13:07:21 john visit
4 2010-02-23 12:21:03 john visit
5 2010-02-23 12:01:03 john visit
6 2010-02-23 11:01:03 john visit
7 2010-02-23 02:01:03 john visit
有了以上数据, john 访问的频率为 13,而期间可能为 12-13。
目标是找到用户最常进行某项活动的时间段/时间。
提前感谢您的所有帮助!
I'm new to SQL Datetime.
Considering I have records in MySQL with Datetime data (as seen below),
what is the best way to get the most frequent occurring HOUR or range/period for these records for a particular user (i.e. john)?
ID | Datetime | User | Activity
0 2010-03-29 13:15:56 john visit
1 2010-03-29 13:13:14 ariel visit
2 2010-03-29 13:09:13 john visit
3 2010-03-29 13:07:21 john visit
4 2010-02-23 12:21:03 john visit
5 2010-02-23 12:01:03 john visit
6 2010-02-23 11:01:03 john visit
7 2010-02-23 02:01:03 john visit
With the above data,
the frequent hour for john doing visit would be 13, while period would be perhaps 12-13.
The goal is to find the period/time that the user does a certain activity most.
Thanks in advance for all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能取决于您使用的数据库,但在 mysql 中:(
请参阅 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html)
It can depend on the database you're using, but in mysql :
(see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html)
使用 Oracle,您可以编写类似于
其他 RDBMS 的代码,使用等效函数从日期时间字段中提取小时部分。
with oracle you can write something like
on other RDBMS use equivalent function to extract hour part from Datetime field.