[英]Sum the values of multiple rows with the same column value in MySQL
我有以下结构:
+====================================================================+
+ City | PartyName | VoteCount | VotePercentage +
+--------------------------------------------------------------------+
+ City1 | Party1 | 871 | 1.2 +
+ City2 | Party1 | 580 | 2.3 +
+ City3 | Party1 | 149 | 1.2 +
+ City1 | Party2 | 234 | 0.2 +
+ City2 | Party2 | 533 | 1.3 +
+ City3 | Party2 | 655 | 2.2 +
+====================================================================+
该表显示了每个城市每个政党的投票数。 我需要一个查询,它总结了每一方的所有投票数和百分比。 所以上表应该是这样的:
+=======================================================+
+ PartyName | VoteCount | VotePercentage +
+-------------------------------------------------------+
+ Party1 | 1600 | 4.7 +
+ Party2 | 1422 | 3.7 +
+=======================================================+
因此,它应该省略城市并总结每一方的投票数和百分比。 我目前的查询:
SELECT City, PartyName, VoteCount, VotePercentage
FROM elections
ORDER BY PartyName
我已经尝试了GROUP BY PartyName,但它只显示了应该出现的行的一小部分。 我怎么能得到上表中的结果?
使用此作为您的查询:
SELECT PartyName, SUM(VoteCount), SUM(VotePercentage)
FROM elections
GROUP BY PartyName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.