亲爱的宝子们好久不见,今天得闲给大家分享一下大屏设计时常用到得统计所占百分比;
这里为了方便大家实操我直接给大家匆匆建立了两个表格sql语句如下:
CREATE table t_group( id int(11) ,name varchar(50));
alter table t_group add primary key (id);
create table t_score( id int(11), g_id int(11), score int(11));
alter table t_score add primary key (id);
insert into t_group (id, name) values (‘1’, ‘a’);
insert into t_group (id, name) values (‘2’, ‘b’);
insert into t_group (id, name) values (‘3’, ‘c’);
insert into t_group (id, name) values (‘4’, ‘d’);
insert into t_group (id, name) values (‘5’, ‘e’);
insert into t_score (id,g_id, score) values (‘1′,’1′, ’81’);
insert into t_score (id,g_id, score) values (‘2′,’1′, ’73’);
insert into t_score (id,g_id, score) values (‘3′,’2′, ’78’);
insert into t_score (id,g_id, score) values (‘4′,’3′, ’92’);
上面表格创建完成并且插入数据后,我们要对两个表格进行操作了(为了大家能够更好的理解我会用不同颜色进行标注);
select tg.name , sum(ts1.score) / (select sum(ts.score) from t_score ts) * 100 as perc
from t_score ts1 join t_group tg on ts1.g_id = tg.id
group by tg.name
运行后会打印出如下内容:通常情况下我们会进行排序,记得sql语句结尾加上 order by XXX字段名 (正序) + desc (倒序)
select tg.name, sum(ts.score)* 100 /(select sum(ts1.score)from t_score ts1) as c
from t_group tg join t_score ts on tg.id = ts.g_id
group by tg.name order by c DESC ;
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/280707.html