0

我在 oracle SQL 查询中有以下代码。此代码查找成员的用户名以及他们每年投票的次数。我该如何修改它以使其仅显示投票次数最多的用户?

SELECT username, count(username), extract(year from voteDate) as vote_year,
max(count(*)) over (partition by extract(year from voteDate)) as Max_votes
FROM rankingInfo NATURAL JOIN memberinfo
GROUP BY username, extract(year from voteDate);
4

1 回答 1

1

如果我正确理解您的查询,您max_votes将返回您想要的最大票数,并count(username)返回每个用户的票数。如果是这样,您可以将结果放在子查询中,然后添加WHERE条件:

SELECT * 
FROM (
   SELECT username, count(username) votes, extract(year from voteDate) as vote_year,
       max(count(*)) over (partition by extract(year from voteDate)) as Max_votes
   FROM rankingInfo NATURAL JOIN memberinfo
   GROUP BY username, extract(year from voteDate)
) T
WHERE votes = max_votes
于 2013-06-03T13:52:11.063 回答