Postgres windows function with aggregate group by
问题 I want to get a list of email domains and the top user within each domain. My approach is to sum the questions per email grouped by the domain and then get the top user with a window function. However this does not work: 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