使用 DATE_FORMAT 别名的 MySQL 子查询计数
在过去的几个小时里,我一直在用头撞墙,试图让这个查询正常工作,但没有成功。
我有一个名为viewer_log 的表。
每行都是一个页面视图,附有一个 session_id 和时间戳。
我无法将时间戳格式化为一天,按该天分组,然后计算每天的唯一会话数。
到目前为止,对于该查询(也花了很长时间才能工作)是:
SELECT day, COUNT(*) as unique_sessions from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS day FROM viewer_log GROUP BY `viewer_session_id` HAVING day='2011-03-23') AS sessions;
您会注意到这一天是硬编码的,因为
mysql> SELECT day, COUNT(*) as unique_sessions from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS day FROM viewer_log GROUP BY `viewer_session_id` HAVING day='2011-03-23') AS sessions;
+------------+-----------------+
| day | unique_sessions |
+------------+-----------------+
| 2011-03-23 | 21 |
+------------+-----------------+
我想扩展这些结果而不必指定日期,这就是我遇到问题的地方并且无法在互联网上找到答案...我确信它就在那里,但不确定我在寻找什么。
EG:
+------------+-----------------+
| day | unique_sessions |
+------------+-----------------+
| 2011-03-21 | 14 |
| 2011-03-22 | 11 |
| 2011-03-23 | 21 |
+------------+-----------------+
我在超越这一点时遇到了很多麻烦,我尝试将表与其自身的副本连接起来,但通常会遇到未知的列名,或者我认为应该显示的结果是错误的:
mysql> select DATE_FORMAT(v.timestamp, "%Y-%m-%d") AS date, count(sessions) as rows from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS date FROM viewer_log GROUP BY viewer_session_id) AS s JOIN viewer_log v ON (s.date=date) GROUP BY date;
+------------+--------+
| date | rows |
+------------+--------+
| 0000-00-00 | 2551 |
| 0000-00-00 | 20408 |
| 0000-00-00 | 20408 |
| 0000-00-00 | 5102 |
| 0000-00-00 | 2551 |
| 0000-00-00 | 2551 |
| 0000-00-00 | 63775 |
| 0000-00-00 | 211733 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 51020 |
| 0000-00-00 | 48469 |
| 0000-00-00 | 91836 |
| 0000-00-00 | 86734 |
| 0000-00-00 | 33163 |
| 0000-00-00 | 45918 |
| 0000-00-00 | 28061 |
| 0000-00-00 | 86734 |
| 0000-00-00 | 76530 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 33163 |
+------------+--------+
22 rows in set, 1 warning (0.63 sec)
I've been banging my head against the wall for the past hours trying to get this single query to work but having no success.
I have a single table called viewer_log.
Each row is a pageview with a session_id and timestamp attached to it.
I am having troubles formatting the timestamp to a day, grouping by that day, but then counting the number of UNIQUE sessions per day.
So far, for that query (which also took a long time to get working) is:
SELECT day, COUNT(*) as unique_sessions from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS day FROM viewer_log GROUP BY `viewer_session_id` HAVING day='2011-03-23') AS sessions;
You will notice the day is hardcoded, giving
mysql> SELECT day, COUNT(*) as unique_sessions from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS day FROM viewer_log GROUP BY `viewer_session_id` HAVING day='2011-03-23') AS sessions;
+------------+-----------------+
| day | unique_sessions |
+------------+-----------------+
| 2011-03-23 | 21 |
+------------+-----------------+
I would like to expand these results without having to specify the day, which is where I am running into problems and not able to find an answer on the internet... I'm sure it's out there, not sure what I'm looking for.
EG:
+------------+-----------------+
| day | unique_sessions |
+------------+-----------------+
| 2011-03-21 | 14 |
| 2011-03-22 | 11 |
| 2011-03-23 | 21 |
+------------+-----------------+
I am having lots of trouble moving past this point, I have tried joining the table with a copy of its self but usually run into an unknown column name OR the results I THINK should be showing up are wrong:
mysql> select DATE_FORMAT(v.timestamp, "%Y-%m-%d") AS date, count(sessions) as rows from (SELECT COUNT(viewer_session_id) AS sessions, DATE_FORMAT(timestamp, "%Y-%m-%d") AS date FROM viewer_log GROUP BY viewer_session_id) AS s JOIN viewer_log v ON (s.date=date) GROUP BY date;
+------------+--------+
| date | rows |
+------------+--------+
| 0000-00-00 | 2551 |
| 0000-00-00 | 20408 |
| 0000-00-00 | 20408 |
| 0000-00-00 | 5102 |
| 0000-00-00 | 2551 |
| 0000-00-00 | 2551 |
| 0000-00-00 | 63775 |
| 0000-00-00 | 211733 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 51020 |
| 0000-00-00 | 48469 |
| 0000-00-00 | 91836 |
| 0000-00-00 | 86734 |
| 0000-00-00 | 33163 |
| 0000-00-00 | 45918 |
| 0000-00-00 | 28061 |
| 0000-00-00 | 86734 |
| 0000-00-00 | 76530 |
| 0000-00-00 | 53571 |
| 0000-00-00 | 33163 |
+------------+--------+
22 rows in set, 1 warning (0.63 sec)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
您可以添加一个 WHERE 条件,例如
假设时间戳是 mysql 时间戳而不是 unix 时间戳
Try
You can add a WHERE condition like
This is assuming the timestamp is a mysql timestamp and not a unix timestamp
谢谢,修改你的建议我最终做的是:
干杯!
Thanks, modifying your suggestion what I ended up doing was:
Cheers!