我不完全确定我理解你的问题,但可能是这样的?
SELECT username,
extract(year from voteDate) as vote_year,
count(*) as votes_per_year,
max(count(*)) over (partition by extract(year from voteDate)) as max_votes_per_year
FROM Votes vt
GROUP BY username, extract(year from voteDate);
这将为您提供每个用户和年份的计数以及每一行中每年的最大计数。(请注意,成员表不是必需的,因为您显然不需要其中的任何列)。
如果您需要在没有人投票(或某个特定用户未投票)的年份显示零计数,那么您将需要这样的东西:
with years as (
select 2009 + level as year
from dual
connect by level <= 21
)
SELECT username,
y.year,
count(vt.username) as votes_per_year,
max(count(vt.username)) over (partition by y.year) as max_votes_per_year
FROM years y
LEFT JOIN Votes vt on y.year = extract(year from vt.voteDate)
GROUP BY username, y.year
order by y.year, username;
with ...
这将使用公用表表达式 ( ) “即时”生成 2010 年到 2030 年之间的年份列表。您可以调整 CTE 以延长或缩小年数。