有一张表,记录游戏用户id和它的等级,让你计算 通关率=对每个等级,大于该等级的人数/大于等于该等级的人数。
解法:
首先统计每个等级人数到临时表(用group by);
create table tmp select count(user_id) as cnt, grade from users group by grade;
然后计算每个等级的通关率(用临时表自连接):
select tmp2.grade, tmp2.cnt/sum(tmp1.cnt) as gl from tmp as tmp1, tmp as tmp2 where tmp1.grade >= tmp2.grade group by tmp2.grade;