MySQL Query for Average of last 2 attempts

后端 未结 5 1482
悲&欢浪女
悲&欢浪女 2021-01-23 07:27

I have a table:

quiz userid  attempt grade

1      3        1     33

2      3        1     67

1      3        2     90

10     3        4     20

2      3              


        
5条回答
  •  忘掉有多难
    2021-01-23 08:19

    First you need to select just the two rows with highest attempts, which is what the subquery does, then average the grades from the derived table.

    SELECT 
        AVG(grade) 
    FROM (
          SELECT 
              grade 
          FROM 
              `table`
          ORDER BY attempt DESC 
          LIMIT 2
         ) t;
    

    This will give you a result of 55.0000, which is the average of 90 and 20, the grades for the last two attempts.

提交回复
热议问题