我想获取电子邮件域列表和每个域中的顶级用户。我的方法是对按域分组的每封电子邮件的问题求和,然后使用窗口函数获取顶级用户。但是,这不起作用:
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_...
^
我真的不明白为什么会这样。我很确定应该能够对聚合结果使用窗口函数。请指教!
谢谢,克里斯托弗