0

感谢您尝试提供帮助。我有一个名为帖子的表格,其中每个帖子都有一行。一篇文章有​​一个 post_date(unix 时间戳格式)和一个 author_id。

我试图获取按月和年分组的一个月内发布 5 次或更多次的唯一作者的数量。

我现在按月和年对唯一作者的查询(没有该月内 5 个或更多帖子的过滤器)是:

select
    month(from_unixtime(p.post_date)) as month,
    year(from_unixtime(p.post_date)) as year,
    count(distinct p.author_id)
from posts p
group by month,year
order by year desc,month desc

你能帮我添加一个过滤器,只计算在给定月份有 5 个或更多帖子的作者吗?

更新

以下查询有效,但速度极慢,即使仅添加本月和本年的谓词也是如此。有人能想出更好的方法吗?

select
    month(from_unixtime(p.post_date)) as month,
    year(from_unixtime(p.post_date)) as  year,
    count(distinct p.author_id)
from posts p 
   where (
     select count(*) from posts p2 where p2.author_id=p.author_id
     and month(from_unixtime(p.post_date))=month(from_unixtime(p2.post_date))
     and year(from_unixtime(p.post_date))=year(from_unixtime(p2.post_date))
   )>=5
 group by month,year order by year desc,month desc
4

1 回答 1

1

您可以使用 count >5 。它将解决问题

  select month(from_unixtime(p.post_date)) as month,
    year(from_unixtime(p.post_date)) as year,
    count( p.author_id) c from posts p 
    group by author , month,year having c >5 
    order by year desc,month desc 
于 2013-05-31T04:31:19.510 回答