TL3の進行を再現するクエリの作成に関するヘルプ

こんにちは、皆さん。

当コミュニティ内では、特定の常連ユーザーを「PosBuddy」に昇格させる制度があります。PosBuddyとは、当社のブランドトーンを体現し、質問への回答、会話の促進、新規メンバーへの歓迎において優れた能力を示す常連ユーザーのことです。

この制度により、これらのユーザーは TL4 に昇格します。 彼らはコミュニティ内に独自のプライベートグループを持ち、投稿を閉鎖・結合・移動させる権限(つまり、コミュニティの整理整頓)を有しています。また、これらのメンバーには、無料製品、グッズ、開発ショーケースへの招待など、多くの素晴らしい特典を提供しています。そのため、TL3 まで昇格してコミュニティに貢献するための大きなインセンティブがあることは想像にお任せします。

私たちはしばしば、「TL3 にほぼ到達しているが、まだ少し足りない」ユーザーを知りたいことがあります。 そのために、毎月数百人の TL2 ユーザーを確認し、TL3 に到達するまでの距離や、Trust Level 3 の要件表を満たしているかどうかをチェックしています。

このプロセスをより簡単にするため、必要な情報のほとんどを単一のビューで表示するデータエクスプローラークエリを用意しています。しかし、以下のフィールドを以下のクエリに含める方法がどうしてもわかりません。

  • 返信されたトピック数
  • フラグ付き投稿数
  • フラグをつけたユーザー
  • 受け取ったいいね:ユニーク日数
  • 受け取ったいいね:ユニークユーザー数
  • 沈黙処分(過去すべて)
  • 停止処分(過去すべて)

どなたかお手伝いいただければ、心から感謝いたします。

-- [params]
-- int :from_days_ago = 0
-- int :duration_days = 100

with
t as (
  select 
    CURRENT_TIMESTAMP - ((:from_days_ago + :duration_days) * (INTERVAL '1 days')) as start,
    CURRENT_TIMESTAMP - (:from_days_ago * (INTERVAL '1 days')) as end
),
pr as (
    select user_id, 
        count(1) as visits,
        sum(posts_read) as posts_read
    from user_visits, t
    where posts_read > 0
    and visited_at > t.start
    and visited_at < t.end
    group by user_id
),
pc as (
  select user_id, 
      count(1) as posts_created
  from posts, t
  where created_at > t.start
  and created_at < t.end
  group by user_id
),
ttopics as (
 select user_id, posts_count
  from topics, t
  where created_at > t.start
  and created_at < t.end
),
tc as (
  select user_id, 
      count(1) as topics_created
  from ttopics
  group by user_id
),
twr as (
  select user_id, 
      count(1) as topics_with_replies
  from ttopics
  where posts_count > 1
  group by user_id
),
tv as (
 select user_id, 
        count(distinct(topic_id)) as topics_viewed
  from topic_views, t
  where viewed_at > t.start
  and viewed_at < t.end
  group by user_id
),
likes as (
  select 
      post_actions.user_id as given_by_user_id, 
      posts.user_id as received_by_user_id
  from t, post_actions
  left join posts
  on post_actions.post_id = posts.id
  where post_actions.created_at > t.start
  and post_actions.created_at < t.end
  and post_action_type_id = 2

),
lg as (
  select given_by_user_id as user_id, 
      count(1) as likes_given
  from likes
  group by user_id
),
lr as (
  select received_by_user_id as user_id, 
      count(1) as likes_received
  from likes
  group by user_id
),
e as (
  select email, user_id
  from user_emails u
  where u.primary = true
)
select pr.user_id,
       coalesce(visits,0) as "Visits",
       coalesce(topics_viewed,0) as "Topics Viewed",
       coalesce(posts_read,0) as "Posts Read", 
       coalesce(posts_created,0) as "Posts Created",
       coalesce(topics_created,0) as "Topics Created",
       coalesce(topics_with_replies,0) as "Topics with Replies",
       coalesce(likes_given,0) as "Likes Given",
       coalesce(likes_received,0) as "Likes Received"
from pr
left join tv using (user_id)
left join pc using (user_id)
left join tc using (user_id)
left join twr using (user_id)
left join lg using (user_id)
left join lr using (user_id)
left join e using (user_id)
left join users on pr.user_id = users.id
order by 
  visits desc, 
  posts_read desc, 
  posts_created desc
「いいね!」 1