Average of latest N records per group
问题 My current application calculates a point average based on all records for each user: SELECT `user_id`, AVG(`points`) AS pts FROM `players` WHERE `points` != 0 GROUP BY `user_id` The business requirement has changed and I need to calculate the average based on the last 30 records for each user. The relevant tables have the following structure: table: players; columns: player_id, user_id, match_id, points table: users; columns: user_id The following query does not work, but it does demonstrate