MySQL在一个查询中获取组和avarage的SUM

时间:2018-01-14 14:29:57

标签: mysql sql join inner-join self-join

我有一张桌子:

+--------+------------+---------+
 id      | day        | status  |
+--------+------------+---------+
 1       | 2018-01-01 |  FAIL   |
 2       | 2018-01-02 | SUCCESS |
 3       | 2018-01-02 | SUCCESS |
 4       | 2018-01-02 | SUCCESS |
 5       | 2018-01-03 | SUCCESS |
 6       | 2018-01-03 |  FAIL   |
 7       | 2018-01-03 |  FAIL   |
 8       | 2018-01-04 | SUCCESS |
 9       | 2018-01-04 | SUCCESS |
 10      | 2018-01-05 | SUCCESS |
 11      | 2018-01-05 | SUCCESS |
 12      | 2018-01-05 | SUCCESS |
+--------+------------+---------+

我必须创建一个包含下一个信息的查询:

  1. 每天失败的次数
  2. 每天的成功次数
  3. 每天的成功+失败次数
  4. 每天成功状态的百分比
  5. 从第四个记录开始,每天从前三天获得SUCCESS百分比的平均值
  6. 我创建了一个查询以获取1-4分的信息:

    SELECT day, fails, success, fails + success AS total, 
    (success*100/(success+fails)) AS success_percent
    FROM (
     SELECT day, 
     SUM(IF(status='FAIL',1,0)) AS fails, 
     SUM(IF(status='SUCCESS',1,0)) AS success 
     FROM statuses
     GROUP BY day)  AS inner_query;
    

    但我不知道如何获得每条记录的先前平均值列

0 个答案:

没有答案