MySQL Forums
Forum List  »  Newbie

Re: SELECT SUM() with only top 4 results
Posted by: Peter Brawley
Date: March 27, 2020 09:27AM

Unfortunately correlated subqueries like yours introduce referencing problems of the sort you tripped over, and often perform badly.

If you have MySQL 8, the windowing functions Row_Number() Over... and Rank() give an elegant solution eg ...

SELECT tray_id, score, `rank`
FROM ( 
  SELECT 
    tray_id, score, 
    ROW_NUMBER() OVER( 
      PARTITION BY tray_id 
      ORDER BY tray_id,score 
    ) AS `rank`
  FROM scores
  ORDER BY tray_id
) AS tmp 
WHERE tmp.`rank` <= 4   -- TOP 4
ORDER BY tray_id, score;

Note `rank` needs backticks round it since presence of the Rank() function makes it a reserved word since 8.0.

The query's explained under "Within-group quotas (Top N per group)" at https://www.artfulsoftware.com/queries.php, as are top-N queries for pre-8.

Options: ReplyQuote


Subject
Written By
Posted
Re: SELECT SUM() with only top 4 results
March 27, 2020 09:27AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.