mysql - include only the first group in query results -
given schema
the following query
select a.user_id, a.date_created, avg(ai.level) level assessment join assessment_item ai on a.id = ai.assessment_id group a.user_id, a.date_created;
returns these results
user_id, a.date_created, level 1, "2015-07-13 18:26:00", 4.0000 1, "2015-07-13 19:04:58", 6.0000 13, "2015-07-13 18:26:00", 2.0000 13, "2015-07-13 19:04:58", 3.0000
i change query such earliest result returned each user. in other words, following should returned instead
user_id, a.date_created, level 1, "2015-07-13 18:26:00", 4.0000 13, "2015-07-13 18:26:00", 2.0000
a possible solution: use user variables 'index' rows, , filter first row:
select user_id, date_created, level ( select a.date_created, a.level , @idx := (case when a.user_id = @uid @idx + 1 else 1 end) idx , @uid := a.user_id user_id (select @uid := 0, @idx := 0) init, ( select a.user_id, a.date_created, avg(ai.level) level assessment join assessment_item ai on a.id = ai.assessment_id group a.user_id, a.id order user_id, date_created ) ) b idx = 1
Comments
Post a Comment