sql中统计百分比

SELECT COUNT(reg_city) AS customerCount,a.reg_city ,
(CAST(CONVERT(100 * CAST(COUNT(*) AS DECIMAL(10,2)) / CAST((SELECT COUNT(*) FROM 表名 ) AS DECIMAL(10,2)), DECIMAL(10,2)) AS CHAR)) AS percentage
FROM 表名1 a LEFT JOIN 表名2 b ON a.id=b.id GROUP BY a.reg_city ORDER BY customerCount DESC ;

原文地址:https://www.cnblogs.com/mimeng/p/14525006.html