mysql如何分组统计并求出百分比
作者:木木-木 时间:2024-01-22 02:07:51
mysql分组统计并求出百分比
1、mysql 分组统计并列出百分比
SELECT
point_id,
pname_cn,
play_num,
round( play_num / total * 100, 2 ) as `ratio`
FROM
(
SELECT
*
FROM
( SELECT
ANY_VALUE ( `point_id` ) AS point_id,
ANY_VALUE ( `pname_cn` ) AS pname_cn,
sum( `play` ) AS play_num
FROM
`dt_collect_antique` WHERE`add_time` BETWEEN '2020-07-02' AND '2020-07-05' GROUP BY `point_id` ) t1
INNER JOIN
( SELECT
sum( `play` ) AS total
FROM
`dt_collect_antique` WHERE`add_time` BETWEEN '2020-07-02' AND '2020-07-05'
) t2 ON 1 = 1
) t
ORDER BY
`play_num` DESC
LIMIT 0, 10;
--查出符合条件并且分组, 统计出每组数量
SELECT
ANY_VALUE ( `point_id` ) AS point_id,
ANY_VALUE ( `pname_cn` ) AS pname_cn,
sum(`like`) as like_num
FROM
`dt_collect_antique` WHERE`add_time` BETWEEN '2020-07-02' AND '2020-07-05' GROUP BY `point_id` ) t1
--查出符合条件,总数量
(SELECT
sum( `play` ) AS total
FROM
`dt_collect_antique` WHERE`add_time` BETWEEN '2020-07-02' AND '2020-07-05'
) t2
2、按年龄段分组,并求个年龄段占比
SELECT
age_group,
age_total,
round( age_total / total * 100, 2 ) as `ratio`
FROM
(
SELECT
*
FROM
( SELECT
SUM(total) AS age_total,
CASE
WHEN age >= 0 AND age < 18 THEN '18岁以下'
WHEN age >= 18 AND age <= 25 THEN '18岁到25岁'
WHEN age >= 26 AND age <= 35 THEN '26岁到35岁'
WHEN age >= 36 AND age <= 45 THEN '36岁到45岁'
WHEN age >= 46 AND age <= 60 THEN '46岁到60岁'
ELSE '60岁以上' END
AS age_group FROM dt_collect_age WHERE `add_time` BETWEEN ".$time[0]." AND ".$time[1]." GROUP BY age_group
) t1
INNER JOIN
( SELECT
SUM( `total` ) AS total
FROM
`dt_collect_age` WHERE `add_time` BETWEEN ".$time[0]." AND ".$time[1]."
) t2 ON 1 = 1
) t
LIMIT 0, 6;
mysql求百分比的几种方法
函数介绍
1、ROUND(X,D)和ROUND(X)
round函数用于数据的四舍五入,x指要处理的数,d是指保留几位小数
round(x) ,其实就是 round(x,0)
d可以是负数,代表指定小数点左边的d位整数位为0,同时小数位均为0
2、FORMAT(X,D)
提供数据内容格式化功能,可以格式化数据为整数或者浮点数,能四舍五入
D为负数时,按0处理
3、LEFT(str,len)
left函数是一个字符串函数,它返回具有指定长度的字符串的左边部分,str为要处理的字符串,len为长度
left函数为字符串截取,不能四舍五入
4、CONCAT(str1,str2,...)
concat函数用于将多个字符串连接成一个字符串
利用round,format,left与concat求百分比
注意:使用left按位截取百分比时,位数要根据需要合理设置,否则容易出现意外BUG
来源:https://blog.csdn.net/qq_38776443/article/details/107167702
标签:mysql,分组统计,百分比
0
投稿
猜你喜欢
Python3.5迭代器与生成器用法实例分析
2022-11-03 14:50:47
HTTP 错误 500.100 - 内部服务器错误 - ASP 错误
2008-09-12 13:07:00
python reduce 函数使用详解
2022-11-01 22:20:57
解决Python3.8用pip安装turtle-0.0.2出现错误问题
2021-04-07 03:51:20
解决Goland 提示 Unresolved reference 错误的问题
2024-02-07 13:32:26
python编程开发之类型转换convert实例分析
2023-03-24 05:48:06
Python中利用aiohttp制作异步爬虫及简单应用
2023-07-21 04:50:40
python实现巡检系统(solaris)示例
2022-03-01 20:32:11
python opencv 读取本地视频文件 修改ffmpeg的方法
2023-10-13 09:10:38
.Net Core服务治理Consul使用服务发现
2023-06-25 07:49:19
python实现二分查找算法
2023-04-04 12:34:40
Mysql字符串截取函数SUBSTRING的用法说明
2024-01-24 14:53:51
快速排序的算法思想及Python版快速排序的实现示例
2021-10-26 18:04:24
PHP实现图片合并的示例详解
2023-06-08 14:14:07
tensorflow输出权重值和偏差的方法
2021-10-01 15:56:13
深入了解Golang中Slice切片的使用
2023-09-25 08:59:08
Python实现迪杰斯特拉算法过程解析
2022-08-14 09:55:42
php7安装openssl扩展方法
2023-11-14 17:34:14
Python爬虫 bilibili视频弹幕提取过程详解
2023-07-05 06:41:25
C#调用python.exe使用arcpy方式
2021-03-30 05:41:13