SQL语言基础

SQL语句分类变量计数与百分比统计

2019-11-22  本文已影响0人  LSim

select reviewAverage,count(reviewAverage) as count from table_nm group by reviewAverage having reviewAverage is not null order by reviewAverage

简单的查询语句大家都比较容易的理解。select 字段A,count(A) as count from 表名 group by 字段A order by count。但是想要百分比出现在后面,需要子查询语句。

*select reviewAverage,count(reviewAverage) as count,concat(round(count(reviewAverage)/(select count(*) from 表名)100,2),’’,’%’) as per from 表名 group by reviewAverage having reviewAverage is not null order by reviewAverage

百分比=count/sum

count(字段)-----------计数count

select count() from 表名 ------- 求出sum

count(字段)/(select count() from 表名)---------百分比

round(百分比100,2)-------保留两位小数

concat(round(百分比100,2),‘’,’%’)-------转换成百分比

只需要把查询结果:concat(round(count(reviewAverage)/(select count() from 表名)100,2),’’,’%’) as per 当成一个字段即可。

有问题,欢迎留言。

每日更新(sql,R,python,databricks,sqlserver),感谢关注!!!

CSDN同步更加精彩---ID:weixin_45098535

CSDN:https://blog.csdn.net/weixin_45098535/article/details/103164812

上一篇下一篇

猜你喜欢

热点阅读