0

助けてくれてありがとう。各投稿の行がある posts というテーブルがあります。投稿には、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 に答える