我需要用SQL计算总数的百分比 我有
customerID, quantity
我尝试过的是:
SELECT
customer_id,
sum(quantity) / (quantity * 100)
FROM MyTable
Group by customer_id
最佳答案
您需要总计来计算百分比,因此请使用子查询。为了避免再次计算每个用户的总数,请交叉连接到一次计算的总数:
select customer_id, quantity * 100 / total
from MyTable
cross join (
select sum(quantity) total
from MyTable) x
关于sql - SQL中同一列的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427688/