0

I have the code below in an oracle SQL query. This code finds the username of a member and how many times they voted each year. How can I modify this so that it only shows the user/s that have voted the highest number of times?

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);

1 Answer 1

1

If I'm understanding your query correctly, your max_votes returns the max votes you want and your count(username) returns the votes for each user. If so, you can put the results in a subquery and then just add WHERE criteria:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.