group by - select the most recent answer for each entry MySQL -


i feel should easy making small mistake somewhere. should add teacher , not coder, i'm not versed in sql. in addition, did @ bunch of questions here , none of them quite worked.

i have table student_answers(id, student_id, question_id, answer, result, date_time) want question_id, answer, result , date_time last answer student entered each question. if answered 3 times question 7, want see last answer , result entered.

for teaching purposes can not update each row re-enter answers.

i tried following queries

select id, question_id, answer, result, date  student_answers student_id = 505 , question_id in (select id test_questions q q.test_id = 37)  group question_id having date = max(date) order student_answers`.`question_id` asc 

but didn't include questions multiple answers @ all, , have me questions student 505 answered once. student 505 answered questions 3 , 4 twice , rest once, , saw results 1, 2 , 5.

i tried query

select     b.*     (         select question_id, max(date) maxdate         tp_student_answers         group question_id     ) inner join     tp_student_answers b on          a.question_id = b.question_id ,          a.maxdate = b.date          , b.student_id = 505          , b.question_id in (select id tp_questions q q.test_id = 37) order     b.question_id 

but on gave me 3 , 4 , none of ones attempted once. appreciated!

this sample of data:

id   student_id question_id answer  result      date   7133     505    1         correct 2012-11-16 09:03:58  7134    505 2      c    wrong   2012-11-16 09:03:58  7135    505 3      e    wrong   2012-11-16 09:03:58  7136    505 3      d    wrong   2013-12-16 09:03:58  7137    505 4      c    correct 2012-11-16 09:03:58  7138    505 4      d    wrong   2013-12-16 09:03:58  7139    505 5       blank   2012-11-16 09:03:58 

when run query see:

7133      505   1         correct 2012-11-16 09:03:58  7134    505 2      c    wrong   2012-11-16 09:03:58  7136    505 3      d    wrong   2013-12-16 09:03:58  7138    505 4      d    wrong   2013-12-16 09:03:58  7139    505 5       blank   2012-11-16 09:03:58  

notice entries 7135 , 7137 omitted since there later answer each of questions

check query: (gives students latest answers questions)

select id, student_id, question_id, answer, result, date_time   (select *,                case                   when (@prevq = question_id , @prevs = student_id)                                        @marker := 0                   else                      @marker := 1                end                   marker,                @prevs := student_id,                @prevq := question_id                   student_answers                 order student_id asc, question_id asc, date_time desc) aview,                (select @prevq = -1, @prevs = -1)  marker = 1; 

and specific student_id , specific question_ids:

select id, student_id, question_id, answer, result, date_time   (select *,                case                   when (@prevq = question_id , @prevs = student_id)                                        @marker := 0                   else                      @marker := 1                end                   marker,                @prevs := student_id,                @prevq := question_id           student_answers              student_id = 501                , question_id in (select id                                      tp_questions q                                     q.test_id = 37)         order student_id asc, question_id asc, date_time desc) aview,        (select @prevq = -1, @prevs = -1)  marker = 1; 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -