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

  • Home
  • SEARCH
  • 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 608783
In Process

The Archive Base Latest Questions

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

I’m writing a particularly troublesome query. It boils down to this: I obtain a

  • 0

I’m writing a particularly troublesome query. It boils down to this:

I obtain a table of the structure:

pid | tid | points

after a pretty large query.

For ease of explanation:

  • pid = problem id
  • tid = team id
  • points = points awarded to that team for that problem.

I want to find the team who has scored max points for a particular pid.

My question is twofold:

  1. If this were a simple table that went by the name teampoints, how do I get the tid that has MAX(points) for every pid? I tried SELECT pid, tid, MAX(points) from teampoints group by pid; but understandably, that would not work

  2. I’ve arrived at this result after a rather large query. If the answer to my first involves selecting data from teampoints again, is there any way to do that without having to calculate the whole table again?

Thanks

PS: I use mysql.


GORY DETAILS: TREAD WITH CAUTION

I have a few tables in my system, their relevant structures being:

users: uid
teams: tid | eid | teamname
teammembers: tid | uid
events: eid
problems: pid | eid
submissions: subid | pid | uid | eid | points | subts

Some notes:
– problems belong to events
– users belong to teams
– submissions belong to problems(pid) and users(uid). the submissions table has a redundant eid field, which can always be determined from the pid.

The use case is:

  • users form teams. users are identified by uid, teams by tid. Team members are stored in teammembers table.
  • users can make submissions, which are stored in submissions table. submissions are awarded points. subts is the unix timestamp of when the submission was made.
  • users can submit multiple times for the same problem. the latest submission (max subts) is counted.

now, in this set up I want to find the teamname that has scored maximum points for any given event (eid).

I hope this makes my situation clear. I wanted to ask only what I needed to know. I furnish these details up an a request in the comments.

EDIT: the query that generated the teampoints table is:

SELECT s.pid, teamlatest.tid, s.points 
  FROM  submissions s, teammembers tm, teams t, 
      (SELECT max(maxts) AS maxts, pid, tid 
         FROM (SELECT latest.maxts, latest.pid, t.tid 
                 FROM submissions s, teams t, teammembers tm,
                     (SELECT max(subts) AS maxts, pid, uid 
                        FROM submissions 
                        WHERE eid=3 AND status='P' 
                        GROUP BY pid, uid
                     ) AS latest
                 WHERE s.uid=latest.uid 
                   AND s.pid=latest.pid 
                   AND s.subts=latest.maxts 
                   AND latest.uid=tm.uid 
                   AND tm.tid=t.tid 
                   AND t.eid=3
              ) AS latestbyteam
         GROUP BY pid, tid) AS teamlatest
  WHERE s.pid=teamlatest.pid 
    AND teamlatest.tid=t.tid 
    AND t.tid=tm.tid 
    AND tm.uid=s.uid 
    AND s.subts=teamlatest.maxts
  • 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:29:46+00:00Added an answer on May 13, 2026 at 5:29 pm
    1. One way:

      SELECT pid, tid, points 
        FROM teampoints
        WHERE (pid, points) IN (
            SELECT pid, MAX(points) 
              FROM teampoints GROUP BY pid
        )
      

      Another, using joins:

      SELECT s1.*
          FROM teampoints AS s1
          LEFT JOIN teampoints AS s2
             ON s1.pid = s2.pid
             AND s1.points < s2.points
          WHERE s2.tid IS NULL
      
    2. You can INSERT INTO a temporary table for the complex query:

      CREATE TEMPORARY TABLE scores (
          pid INT, tid INT, points INT,
          KEY pp (pid, points)
      );
      INSERT INTO scores (pid, tid, points) 
          SELECT <a complex query>
      

      then SELECT the top scorers from that.

    • 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.