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 613237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:58:51+00:00 2026-05-13T17:58:51+00:00

I have a points system setup on my site, where every single point accumulated

  • 0

I have a points system setup on my site, where every single point accumulated is logged in the points table. The structure is simple, p_userid, p_points (how many points accumulated during this action), and p_timestamp.

I wanna display top 3 point accumulating users, for each month. So essentially, it should sum the p_points table for the month, for each user id, and display the top 3 users, grouped into months. The user ids will be joined to a users table, to get actual user names.

What would be the best way to do it? I use php/mysql.

EDIT:
As a possible solution, I could create another column, and log YYYY-MM into it, and simply group it based on that, but thats more data I gotta log, for an already huge table.

EDIT 2:

Data stored as such

INSERT INTO `points` (`point_userid`, `point_points`, `point_code`, `point_date`) VALUES
(8465, 20, 3, 1237337627),
(46745, 20, 3, 1237337678),
(7435, 20, 3, 1237337733),
(46565, 20, 3, 1237337802),
(4466, 20, 3, 1237337836),
(34685, 20, 3, 1237337885),
(8544, 20, 3, 1237337908),
(6454, 20, 3, 1237337998),
(45765, 20, 3, 1237338008),
(3476, 20, 3, 1237338076);
  • 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-13T17:58:51+00:00Added an answer on May 13, 2026 at 5:58 pm

    This isn’t easy in MySQL.

    First you need to create a table of variables, one for storing the current group, and one for storing the current row number in the group. Initialize them both to NULL.

    Then iterate group by month and select all rows ordered by score and select the current rown number and increase it. If the group changes, reset the row number to one.

    Then put all this in a subselect and in the outer select, select all rows with rownumber <= 3.

    You could use this query:

    SELECT month, p_userid, points FROM (
        SELECT
            *,
            (@rn := CASE WHEN month = @last_month THEN @rn + 1 ELSE 1 END) AS rn,
            (@last_month := month)
        FROM (
            SELECT p_userid, month(p_timestamp) AS month, SUM(p_points) AS points
            FROM Table1, (SELECT @last_month := NULL, @rn := 0) AS vars
            GROUP BY p_userid, month(p_timestamp)
            ORDER BY month, points DESC
        ) AS T1
    ) AS T2
    WHERE rn <= 3
    

    Result:

    Month User Score
    1     4    7
    1     3    5
    1     2    4
    2     4    17
    2     5    10
    2     3    6
    

    Test data:

    CREATE TABLE Table1 (p_userid INT NOT NULL,
                         p_points INT NOT NULL,
                         p_timestamp TIMESTAMP NOT NULL);
    
    INSERT INTO Table1 (p_userid, p_points, p_timestamp) VALUES
    (1, 1, '2010-01-01'),
    (1, 2, '2010-01-02'),
    (1, 3, '2010-02-01'),
    (2, 4, '2010-01-01'),
    (3, 5, '2010-01-01'),
    (3, 6, '2010-02-01'),
    (4, 7, '2010-01-01'),
    (4, 8, '2010-02-01'),
    (4, 9, '2010-02-02'),
    (5, 10, '2010-02-02');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.