如何计算MYSQL中的百分比?

时间:2017-09-26 12:26:12

标签: mysql sql

我不理解,为什么我得到0.01%而不是100%,请告知

这是我的疑问:

SELECT Total_Price,concat(round(total_price/(SUM(total_price)*100),2),'%') AS percentage
FROM Sales
WHERE Department IN ('SPORT','MUSIC')
AND DATE BETWEEN '2016-01-01' AND '2016-03-30'

由于

1 个答案:

答案 0 :(得分:0)

尝试将总数作为子查询...

select t1.total_price, (t1.total_price/t2.grand_total)*100 as pc
from MyTable t1
inner join
(
select sum(tx.total_price) as grand_total
from MyTable tx
AND tx.DATE BETWEEN '2016-01-01' AND '2016-03-30'
) t2
on 1=1
WHERE Department IN ('SPORT','MUSIC')
AND DATE BETWEEN '2016-01-01' AND '2016-03-30'