SQL: Need to get MAX values from one table and divide by the total number of rows in another table -
i have table bunch of scores lessons, on user user basis
------------------------ |uid |lesson_id |score | ------------------------ |1 |0 |20 | |1 |0 |25 | |1 |0 |15 | |1 |0 |40 | |1 |1 |70 | |1 |0 |10 | |1 |1 |20 | |1 |1 |55 | |1 |1 |55 | |1 |0 |5 | |1 |2 |65 | ------------------------
i have table of possible lessons can scored:
------------ |lesson_id | ------------ |0 | |1 | |2 | |3 | |4 | |5 | ------------
i need calculate maximum score each lesson in second table scores in first table, , take average of on number of lessons in second table:
so, maximum scores scores table (for user 1):
----------------------- |lesson_id |max_score | ----------------------- |0 |40 | |1 |70 | |2 |65 | -----------------------
i need sum them: 175 , divide total number of lessons in table 2: 6 should give answer 29.16
any ideas how in single statement?
i can average of max values scores table (for user 1) so:
select avg(max_score) avg_max_score ( select uid, lesson_id, max(score) max_score cdu_user_progress , uid =1 group lesson_id ) m
select avg(max_score) ( select lesson.lesson_id, max(isnull(score,0)) max_score lesson left join cdu_user_progress on lesson.lesson_id = cdu_user_progress.lesson_id group lesson.lesson_id ) m
Comments
Post a Comment