MySQL 聚合函数中的其他聚合函数
我有一张桌子,上面有帖子。就像(id
int,date
datetime)。 如何使用一个 sql 请求选择每个月每天的平均帖子数?
谢谢你!
I'm having a table with posts. Like (id
int, date
datetime).
How can I select average posts per day count for each month with one sql request?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以为您完成:
说明:因为您正在聚合上进行聚合,所以无法绕过对查询进行查询:
This should do it for you:
Explanation: Because you are doing an aggregate on an aggregate, there is no getting around doing a query on a query:
您可以这样获取每月的帖子数量:
现在我们需要一个月的天数:
这将获得帖子所在日历月内每天的平均帖子数量。也就是说,当月的平均值将持续上升,直到月底。如果您想要当月的真实平均值,则必须输入条件才能获得真实的经过天数。
You can get the number of posts per month like this:
Now we need the number of days in a month:
This will get the average number of posts per day during the calendar month of the post. That is, averages during the current month will continue to rise until the end of the month. If you want real averages during the current month, you have to put in a conditional to get the true number of elapsed days.