如何计算sql中列中值的百分比?

时间:2016-09-25 00:19:31

标签: mysql sql

我有一个名为text的列,另一个名为' categories'有三个值"肯定","负","中立"。

如何计算类别中每个文本值的百分比? 例如,如果我有3行,1行是正数,1行是负数,1行是中性,查询将产生33%正33%负数和33%中性?

这是我得到的阶段......

SELECT COUNT(category), category  FROM tweets GROUP BY category

3 个答案:

答案 0 :(得分:2)

一种方法

select category, count, count/total percent
  from 
  (
    select category, count(category) count
      from tweets 
     group by category
  ) c JOIN (
    select count(*) total
      from tweets
  ) t

输出:

+----------+-------+---------+
| category | count | percent |
+----------+-------+---------+
| negative |     1 |  0.3333 |
| neutral  |     1 |  0.3333 |
| positive |     1 |  0.3333 |
+----------+-------+---------+
  

......只能返回33%而不是0.3333吗?

select category, count, round(count / total * 100) percent
  from 
  (
    select category, count(category) count
      from tweets 
     group by category
  ) c JOIN (
    select count(*) total
      from tweets
  ) t
+----------+-------+---------+
| category | count | percent |
+----------+-------+---------+
| negative |     1 |      33 |
| neutral  |     1 |      33 |
| positive |     1 |      33 |
+----------+-------+---------+

如果您想添加%,可以使用do concat(round(count / total * 100), '%'),但我强烈建议您在客户端代码中执行此操作(任何格式化)。

答案 1 :(得分:1)

作为一个注释,我认为这更简单地使用一个子查询编写:

select t.category, count(*) / t.total,       -- number
       concat(100 * count(*) / t.total, '%') -- string
from tweets t join
     (select count(*) as total) t
group by category;

如果您知道只有三个类别,我会将它们排成一行:

select avg(category = 'positive') as p_positive,
       avg(category = 'negative') as p_negative
       avg(category = 'neutral') as p_neutral
from tweets t;

此查询使用MySQL功能,即在数字上下文中将布尔表达式视为整数,使用" 1"为真和" 0"为假。

答案 2 :(得分:-1)

只需对当前查询进行一些小修改:

SELECT COUNT(category)/COUNT(*), category FROM tweets GROUP BY category