简体   繁体   English

SQL总计按百分比计算

[英]SQL totals by percentages calculation

I want a SQL query to go from 我想要一个SQL查询

Shop    Regio    target1    sold1    target2    sold2 
shop1   regioA   6          3        10         5
shop2   regioA   4          2        4          2
shop3   regioB   6          0        3          0
shop4   regioC   9          9        8          8
shop5   regioB   8          4        2          1

to

regioC      100%
regioA       50%
regioB       25%

(nevermind the numbers, I just made these up) (没关系,我只是编造的)

I tried using this but it didn't work: 我尝试使用此方法,但是没有用:

SELECT regio, SUM((sold2/target1)+(sold2/target2)) AS total 
FROM  `winkels` GROUP BY `regio` ORDER BY total DESC 

Any ideas how to make it right? 任何想法如何使它正确?

This should work. 这应该工作。 You were trying to add percentages together, instead of dividing by the totals. 您试图将百分比加在一起,而不是除以总计。

SELECT regio, 100*SUM(sold1+sold2)/sum(target1 +target2) AS total 
FROM  `winkels` GROUP BY `regio` ORDER BY total DESC 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM