Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 525707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:42:00+00:00 2026-05-13T08:42:00+00:00

I’m trying to write a query that returns the user ID’s of the top

  • 0

I’m trying to write a query that returns the user ID’s of the top 10 users who gained the most points in the last 7 days on my web app.

I have three tables that, together, have the info I need.

  1. votingapi_votes table. It has a record for every up/down vote on a comment or node.
  2. node table. It can associate a node ID with a user ID so you can figure out who posted the story getting the votes.
  3. comments table, which does the same for comments.

I believe I need to write a query that selects every vote on a comment or node from the last week from the votingapi_vote table.

Here’s the structure of that table:

  • vote_id
  • content_type
  • content_id
  • value
  • value_type
  • tag
  • uid
  • timestamp

So I’d SELECT rows of content_type “node” or “comment” with a Unix timestamp greater than time() – 684000.

Then it needs to

  1. group these votes by “content_id”.

  2. Look up the respective “user_id” values for each content_id in the “node” and “comments” tables so we know who made the nodes and comments.

  3. Calculate how many points total each user_id gained from his nodes and comments.

  4. Sort these user_id’s in reverse order and limits it to displaying only the first 10.

Phew. That seems like what I need to do. Now what does that actual query look like?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:42:01+00:00Added an answer on May 13, 2026 at 8:42 am

    Posting based on OMG Ponies’ answer

    SELECT x.user_id, SUM(x.total_votes)
        FROM (
            SELECT n.user_id, SUM(vav.value) AS total_votes
                FROM NODE n
                JOIN VOTINGAPI_VOTES vav 
                    ON vav.content_id = n.nid
                    AND vav.content_type = 'node'
                WHERE vav.timestamp > NOW() - 684000
                GROUP BY n.user_id
            UNION
            SELECT c.user_id, SUM(vav.value) AS total_votes
                FROM COMMENTS c
                JOIN VOTINGAPI_VOTES vav 
                    ON vav.content_id = c.cid
                    AND vav.content_type = 'comment' 
                WHERE vav.timestamp > NOW() - 684000
                GROUP BY c.user_id
        ) x
        GROUP BY 
            x.user_id
        ORDER BY 
            x.total_votes DESC
        LIMIT 10
    

    The problem with the earlier code is that it returns 2 rows per user, 1 for comment, 1 for node. This code will do another SUM to aggregate it to just 1 number per user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.