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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:02:38+00:00 2026-05-17T21:02:38+00:00

My Schema I have the following tables table notes/example values ———————————————— users ( id

  • 0

My Schema

I have the following tables

table             notes/example values
------------------------------------------------
users (
  id
  email           # "foo@example.com"
)                 

games (           
  id              
  name            # "Space Invaders", "Asteroids", "Centipede"
)                 

players (         
  id              
  name            # "uber dude"
  user_id         # player belongs to user
  game_id         # player belongs to game
)                 

scores (          
  id              
  player_id       # belongs to one player
  value           # 50
  created_at      # "2010-09-10",   "2010-08-05"
  month           # "2010-09",      "2010-08"
)

I need to create two reports.

1) Top players

Best performing players (sum all scores for each player) for the most recent
4 months. Show top 10 for each month.

    2010-07         2010-08           2010-09    2010-10
 1  plyA 5,000 pts  plyB  9,400 pts   ...        ...
    Centipede       Solitaire

 2  plyB 3,600 pts  plyC  8,200 pts   ...        ...
    Asteroids       Centipede       

 3  plyC 2,900 pts  plyA  7,000 pts   ...        ...
    Centipede       Centipede

 4  ...             ...               ...        ...
 5  ...             ...               ...        ...
 6  ...             ...               ...        ...
 7  ...             ...               ...        ...
 8  ...             ...               ...        ...
 9  ...             ...               ...        ...
10  ...             ...               ...        ...

2) Top Users:

Best performing users (sum all scores for each players for each user) for the
most recent 4 months. Show top 10 for each month.

    2010-07           2010-08             2010-09    2010-10
 1  userA 50,000 pts  userB 51,400 pts    ...        ...
 2  userB 40,500 pts  userA 39,300 pts    ...        ...
 3  userC 40,200 pts  userC 37,000 pts    ...        ...
 4  ...               ...                 ...        ...
 5  ...               ...                 ...        ...
 6  ...               ...                 ...        ...
 7  ...               ...                 ...        ...
 8  ...               ...                 ...        ...
 9  ...               ...                 ...        ...
10  ...               ...                 ...        ...

MySQL View helper

For joining purposes, I have a stored view to help query the months for the reports. It will always return the most recent 4 months.

report_months (
  month
)

SELECT * FROM report_months;

2010-07
2010-08
2010-09
2010-10

The Problem

In report #1, for example, I can get the sums pretty easily.

select
  p.name        as player_name,
  g.name        as game_name,
  s.month       as month,
  sum(s.score)  as sum_score

from players  as p

join games    as g
  on g.id = p.game_id

join scores   as s
  on s.player_id = p.id

join report_months as rm  -- handy view helper
  on rm.month = s.month

group by
  p.name, g.name

order by
  sum(s.score) desc

-- I can't do this :(
-- limit 0, 40

However, I can’t simply fetch the top 40 results and spread them across 4 months as this wouldn’t guarantee me 10 for each month.

The Question

How can I modify my query to ensure that I’d get 10 for each month?

  • 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-17T21:02:38+00:00Added an answer on May 17, 2026 at 9:02 pm

    I wouldn’t try to make an SQL query that tabulates by month as you have shown.

    Instead, query the top 10 players per month as rows, not as columns:

    Month    Rank  Player  TotalScore  Game
    2010-07     1    plyA   5,000 pts  Centipede
    2010-07     2    plyB   3,600 pts  Asteroids
    2010-07     3    plyC   2,900 pts  Centipede
    ...
    2010-08     1    plyB   9,400 pts  Solitaire
    2010-08     2    plyC   8,200 pts  Centipede
    2010-08     3    plyA   7,000 pts  Centipede
    ...
    

    This becomes a greatest-n-per-group problem, where n is 10.

    CREATE VIEW PlayerScoresByMonth AS
      SELECT month, player_id, SUM(value) AS score
      FROM scores
      GROUP BY month, player_id;
    
    SELECT s1.month, COUNT(s2.month)+1 AS Rank, s1.player_id, s1.score AS TotalScore
    FROM PlayerScoresByMonth s1
    LEFT OUTER JOIN PlayerScoresByMonth s2 ON s1.month = s2.month 
      AND (s1.score < s2.score OR s1.score = s2.score AND s1.player_id < s2.player_id)
    GROUP BY s1.month, s1.player_id
    HAVING COUNT(*) < 10
    ORDER BY s1.month, Rank;
    

    (that’s untested but should get you started)

    Then you need to write some application code to fetch the results of this query and separate the lists by month, and present the data however you were going to do that.

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

Sidebar

Related Questions

I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
In a table I have the following schema table1: playerID int primary key, nationalities
I have run across an XML Schema with the following definition: <xs:simpleType name=ClassRankType> <xs:restriction
Given the following schema: CREATE TABLE players ( id BIGINT PRIMARY KEY, name TEXT
I have a database where I store objects. I have the following (simplified) schema
I have an XML schema that includes multiple addresses: <xs:element name=personal_address maxOccurs=1> <!-- address
I have a star schema type data base, with fact tables that have many
Lets say I have the following 2 tables in a database: [Movies] (Scheme: Automatic)
I have a XML schema that I will need to create Java classes for.
I have an XML schema that represents a product in a DB, and I

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.