show 3 most recent weights for each subject? | Ben Bimber | 2011-06-03 13:46 | |||||||||||||||||||||||||||||||||
Status: Closed | |||||||||||||||||||||||||||||||||||
we have a table of weights, with the columns, participantId, date and weight. is there a good way to write a query to show the 3 most recent weights for each subject? i'd be happy with either 3 rows per subject or something like a group_concat of the 3 weights. i was attempting the query below, but this sort of subselect does not seem to be allowed: SELECT d.id, (SELECT group_concat(w.weight) As weights FROM study.weight w WHERE w.id=d.id ORDER BY w.date desc LIMIT 3 ) AS weights FROM study.demographics d otherwise, i believe finding 'most recent weight' means finding the max date per subject, then joining back on itself to find the corresponding weight. if you want the 3 newest weights, doing that 3X would get really ugly. thanks for any help. |
|||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||