【发布时间】:2018-05-15 15:28:22
【问题描述】:
我有一张像下面这样的表格...我需要他们使用表格下方的查询的数据。我需要计算公式为 sum(column3) / total(column3) *100 或 ((0.3333/0.9999)*100) 的百分比。我已经搜索过,但我没有找到任何可以使用 mysql 来完成的事情。甚至有可能吗?谁能给我一些建议?
+----------+---------+----------+--------+
| column1 | column2 | column3 | percentage |
+----------+---------+---------+------------+
| negative | 1 | 0.3333 | % |
| neutral | 1 | 0.3333 | % |
| positive | 1 | 0.3333 | % |
+----------+---------+----------+-----------+
| Total | 3 | 0.9999 | % |
+----------+---------+----------+-----------+
SELECT
column1_text,
sum(column2_number) as 'column2',
sum(column3_number) as 'column3',
percentage_here as 'percentage'
FROM table
GROUP BY column1 ASC WITH ROLLUP
【问题讨论】:
-
虽然可以使用子查询,但我会在应用程序语言中这样做。
-
@PaulSpiegel 我正在使用 php 动态创建表,但不可能这样做......关于子查询的含义?我怎样才能得到汇总结果并将它们除以总和?
标签: mysql sum percentage rollup