从顶级质量会员查询中排除群组

你好,

请问如何从这个查询中移除某些分组?

SELECT 
    sum(p.score) / count(p) as "每篇帖子平均得分", 
    count(p.id) as 帖子数量,
    p.user_id
FROM posts p
JOIN users u ON u.id = p.user_id
WHERE p.created_at >= CURRENT_DATE - INTERVAL '3 month'
  AND u.active
GROUP by user_id, u.views
HAVING count(p.id) > 40
ORDER BY sum(p.score) / count(p) DESC
LIMIT 100

提前感谢!

我并没有真正测试过这个。

-- [参数]
-- int_list :exclude_groups = 12, 2, 3

SELECT 
    sum(p.score) / count(p) as "每帖平均分数", 
    count(p.id) as 帖子数量,
    p.user_id
FROM posts p
JOIN users u ON u.id = p.user_id
LEFT JOIN group_users gu on u.id = gu.user_id
WHERE p.created_at >= CURRENT_DATE - INTERVAL '3 month'
  AND u.active
  AND (gu.group_id not IN (:exclude_groups))
GROUP by p.user_id, u.views
HAVING count(p.id) > 40
ORDER BY sum(p.score) / count(p) DESC
LIMIT 100

感谢 @pfaffman,您太客气了。

我运行了脚本,但发现被排除组的成员仍出现在结果中。当然,这些成员可能属于多个组。有没有办法让该脚本绝对排除 :exclude_groups 列表中所有组的所有成员?

感谢您的考虑。

哦,对了。not in 并没有像我最初设想的那样工作。它基本上不起作用。我认为需要做的首先是查询不在任何组中的用户,然后将该结果与查询的其他部分进行连接。

抱歉。


    -- [参数]
    -- int_list :exclude_groups = 1,2

    SELECT 
        sum(p.score) / count(p) AS "每篇帖子平均得分", 
        count(p.id) AS 帖子数量, 
        p.user_id
    FROM posts p
    JOIN users u ON u.id = p.user_id
    WHERE p.created_at >= CURRENT_DATE - INTERVAL '3 month'
      AND u.active
      AND u.id NOT IN(
        SELECT user_id FROM group_users WHERE group_id IN (:exclude_groups)
      )
    GROUP BY user_id, u.views
    HAVING count(p.id) > 40
    ORDER BY sum(p.score) / count(p) DESC
    LIMIT 100

@RGJ

感谢您对 Discourse 生态系统的贡献,以及作为客户给予我的卓越支持。得知您提供了如此出色的托管服务,我深感振奋!