mysql - include only the first group in query results -


given schema

enter image description here

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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -