select uid,gap,round(av*gap/total,2) as average from (select uid,max(datediff(next,start_time))+1 as gap,avg(num) as av, datediff(max(start_time),min(start_time))+1 as total from (select uid,start_time,lead(start_time) over(partition by uid order by start_time asc) as next, count(start_time) over(partition by uid) as num from exam_record where year(start_time)=2021) as e group by uid having gap > 1) as ee order by gap desc,average desc