MySQL 分组统计并列出百分比
SELECT result, n, round( n / s * 100, 5 ) rat FROM ( SELECT * FROM ( SELECT result, count( 1 ) n FROM sendsms_log GROUP BY result ) t1 INNER JOIN ( SELECT count( 1 ) s FROM sendsms_log ) t2 ON 1 = 1 ) t
其中“SELECT result, count( 1 ) n FROM sendsms_log GROUP BY result”是查出短信发送结果分组统计,“SELECT count( 1 ) s FROM sendsms_log”查出所有记录数,round( n / s * 100, 5 )计算百分比,保留5位小数。效果:
result | n | rat |
至尊 | 100 | 10% |
总代 | 100 | 10% |
市代 | 300 | 30% |
分销 | 500 | 50% |
相关推荐
云中舞步 2020-11-12
杨德龙 2020-11-11
JohnYork 2020-10-16
wangzhaotongalex 2020-09-22
xiaoseyihe 2020-11-16
不要皱眉 2020-10-14
Crazyshark 2020-11-13
K先生 2020-11-10
momode 2020-09-11
思君夜未眠 2020-09-04
点滴技术生活 2020-08-21
MaggieRose 2020-08-19
kevinweijc 2020-08-18
wintershii 2020-08-17
vapaad 2020-08-17
wera00 2020-08-17
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
JimyFengqi 2020-08-16