mysql计算分位值

问题:线上问题分析中需要计算MTTR的分位值(80分位/95分位)

解法:

step1:计算mttr并加上索引

select  @index:=@index + 1 as myindex, (UNIX_TIMESTAMP(resume_time) - UNIX_TIMESTAMP(bug_time))/60 as mttr 
from online_bug inner join (select @index:=0) as tmp on 1=1 
where deleted=0 and (bug_finder like('%变更拦截%') or bug_finder like('线上监测')) and charge>0 order by mttr

得到如下结果:

mysql计算分位值_第1张图片

 

step2:将上一步查询结果做为子表

select max(mttr) from (
    select  @index:=@index + 1 as myindex, (UNIX_TIMESTAMP(resume_time) - UNIX_TIMESTAMP(bug_time))/60 as mttr
    from online_bug inner join (select @index:=0) as tmp on 1=1
    where deleted=0 and (bug_finder like('%变更拦截%') or bug_finder like('线上监测')) and charge>0 order by mttr
    ) as tmp
where myindex <= @`index` * 0.95;

得出95分位结果:

 

你可能感兴趣的:(mysql,mysql,数据库,sql)