Postgres windows函数与聚合组by

时间:2013-09-08 18:01:07

标签: sql postgresql window-functions

我想获取每个域中的电子邮件域和最高用户的列表。我的方法是对按域分组的每封电子邮件的问题求和,然后让顶级用户使用窗口函数。但是这不起作用:

SELECT 
  domain,
  sum(questions_per_email) as questions_per_domain,
  first_value(email) OVER (PARTITION BY domain ORDER BY questions_per_email DESC) as top_user
FROM (
    SELECT email,
           lower(substring(u.email from position('@' in u.email)+1)) as domain,
           count(*) as questions_per_email
      FROM questions q
      JOIN identifiers i ON (q.owner_id = i.id)
      JOIN users u ON (u.identifier_id = i.id)
    GROUP BY email
  ) as per_user
GROUP BY domain, top_user

Postgres给出了以下信息:

ERROR:  column "per_user.questions_per_email" must appear in the GROUP BY clause or be used in an aggregate function
LINE 5: ...t_value(email) OVER (PARTITION BY domain ORDER BY questions_...
                                                             ^

我真的不明白为什么会这样。我非常确定应该能够在聚合结果上使用窗口函数。请指教!

谢谢, 的Kristoffer

1 个答案:

答案 0 :(得分:3)

您可以像这样更改查询:

with cte1 as (
    SELECT email,
           lower(substring(u.email from position('@' in u.email)+1)) as domain
      FROM questions q
      JOIN identifiers i ON (q.owner_id = i.id)
      JOIN users u ON (u.identifier_id = i.id)
), cte2 as (
    select
        domain, email,
        count(*) as questions_per_email,
        first_value(email) over (partition by domain order by count(*) desc) as top_user
    from cte1
    group by email, domain
)
select domain, top_user, sum(questions_per_email) as questions_per_domain
from cte2
group by domain, top_user

<强> sql fiddle demo