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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:13:29+00:00 2026-06-15T13:13:29+00:00

I have a MYSQL table like this: id | userid | score | datestamp

  • 0

I have a MYSQL table like this:

  id |  userid  |  score  |      datestamp      |
-----------------------------------------------------
  1  |    1     |   5     |  2012-12-06 03:55:16
  2  |    2     |   0,5   |  2012-12-06 04:25:21
  3  |    1     |   7     |  2012-12-06 04:35:33
  4  |    3     |   12    |  2012-12-06 04:55:45
  5  |    2     |   22    |  2012-12-06 05:25:11
  6  |    1     |   16,5  |  2012-12-06 05:55:21
  7  |    1     |   19    |  2012-12-06 13:55:16
  8  |    2     |   8,5   |  2012-12-07 06:27:16
  9  |    2     |   7,5   |  2012-12-07 08:33:16
  10 |    1     |   10    |  2012-12-07 09:25:19
  11 |    1     |   6,5   |  2012-12-07 13:33:16
  12 |    3     |   6     |  2012-12-07 15:45:44
  13 |    2     |   4     |  2012-12-07 16:05:16
  14 |    2     |   34    |  2012-12-07 18:33:55
  15 |    2     |   22    |  2012-12-07 18:42:11

I would like to display user scores like this:
if a user on a certain day has more than 3 scores it would get only highest 3, repeat that for every day for this user and then add all days together. I want to display this sum for every user.

EDIT:
So in the example above for user 1 on 06.12. I would add top 3 scores together and ignore 4th score, then add to that number top 3 from the next day and so on. I need that number for every user.

EDIT 2:
Expected output is:

  userid |   score  
--------------------
    1    |    59    //19 + 16.5 + 7 (06.12.) + 10 + 6.5 (07.12.)
    2    |    87    //22 + 0.5 (06.12.) + 34 + 22 + 8.5 (07.12.)
    3    |    18    //12 (06.12.) + 6 (07.12.)

I hope this is more clear 🙂

I would really appreciate the help because I am stuck.

  • 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-06-15T13:13:30+00:00Added an answer on June 15, 2026 at 1:13 pm

    Please take a look at the following code, if your answer to my comment is yes 🙂 Since your data all in 2012, and month of november, I took day.

    • SQLFIDDLE sample

    Query:

    select y.id, y.userid, y.score, y.datestamp 
    from (select id, userid, score, datestamp 
          from scores
          group by day(datestamp)) as y    
    where (select count(*) 
           from (select id, userid, score, datestamp
                 from scores group by day(datestamp)) as x
           where y.score >= x.score
           and y.userid = x.userid
          ) =1 -- Top 3rd, 2nd, 1st    
    order by y.score desc
    ;
    

    Results:

    ID  USERID  SCORE   DATESTAMP
    8   2       8.5 December, 07 2012 00:00:00+0000
    20  3       6   December, 08 2012 00:00:00+0000
    1   1       5   December, 06 2012 00:00:00+0000
    

    Based on your latter updates to question.
    If you need some per user by year/month/day and then find highest, you may simply add aggregation function like sum to the above query. I am reapeating myself, since your sample data is for just one year, there’s no point group by year or month. That’s why I took day.

    select y.id, y.userid, y.score, y.datestamp 
    from (select id, userid, sum(score) as score,
          datestamp 
    from scores
    group by userid, day(datestamp)) as y    
    where (select count(*) 
    from (select id, userid, sum(score) as score
          , datestamp
    from scores
    group by userid, day(datestamp)) as x
    where y.score >= x.score
    and y.userid = x.userid
    ) =1 -- Top 3rd, 2nd, 1st    
    order by y.score desc
    ;
    

    Results based on sum:

    ID  USERID  SCORE   DATESTAMP
    1   1       47.5    December, 06 2012 00:00:00+0000
    8   2       16      December, 07 2012 00:00:00+0000
    20  3       6       December, 08 2012 00:00:00+0000
    

    UPDATED WITH NEW SOURCE DATA SAMPLE

    Simon, please take a look at my own sample. As your data was changing, I used mine.
    Here is the reference. I have used pure ansi style without any over partition or dense_rank.
    Also note the data I used are getting top 2 not top 3 scores. You can change is accordingly.

    Guess what, the answer is 10 times simpler than the first impression your first data gave….

    SQLFIDDLE

    Query to 1:
    — for top 2 sum by user by each day

    SELECT userid, sum(Score), datestamp
    FROM scores t1
    where 2 >=
    (SELECT count(*) 
     from scores t2
     where t1.score <= t2.score
     and t1.userid = t2.userid
     and day(t1.datestamp) = day(t2.datestamp)
     order by t2.score desc)
    group by userid, datestamp 
    ;
    

    Results for query 1:

    USERID  SUM(SCORE)  DATESTAMP
    1       70      December, 06 2012 00:00:00+0000
    1       30      December, 07 2012 00:00:00+0000
    2       22      December, 06 2012 00:00:00+0000
    2       25      December, 07 2012 00:00:00+0000
    3       30      December, 06 2012 00:00:00+0000
    3       30      December, 07 2012 00:00:00+0000
    

    Final Query:
    — for all two days top 2 sum by user

    SELECT userid, sum(Score)
    FROM scores t1
    where 2 >=
    (SELECT count(*) 
     from scores t2
     where t1.score <= t2.score
     and t1.userid = t2.userid
     and day(t1.datestamp) = day(t2.datestamp)
     order by t2.score desc)
    group by userid
    ;
    

    Final Results:

    USERID  SUM(SCORE)
    1      100
    2      47
    3      60
    

    Here goes a snapshot of direct calculations of data I used.

    enter image description here

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

Sidebar

Related Questions

I have a MYSQL table like this example: [ID]--[Date]--[UserID]--[Username]--[String] 1 12-10-06 02:35:01 25 Jack
I have a table like this: ActionTime UserID Score 2011-08-15 12:06:00 1 14 2011-08-15
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a mysql table that looks like this: id col2 col3 col4 1
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table that looks like this: `id` int(10) unsigned NOT NULL
I have this mysql table called comments which looks like this: commentID parentID type
I have a MySQL table that looks something like this: +-----+------------+ | id |
I have a mySQL table that has a column like this: ID ----- 0352

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.