2016-09-25 106 views
0

我有一个名为文本的列,另一个名为“类别”,有三个值“正面”,“负面”,“中性”。如何计算sql中列中值的数量的百分比?

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

这是我到了舞台......

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

回答

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 | 
+----------+-------+---------+ 

...是否有可能? o只返回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 | 
+----------+-------+---------+ 

如果你想添加%可以用做concat(round(count/total * 100), '%')但我强烈建议在客户端代码做(任何一种格式)。

+0

感谢您的答案@peterm是否有可能只返回33%而不是0.3333? –

+0

太好了,谢谢@peterm –

0

只是一个小修改您的当前查询:

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

'COUNT(category)/ COUNT(*)'总是会返回'1',假设'category'不包含空值 – peterm

+0

确认,你说得对。 – kiastu

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; 

这q uery使用MySQL特性将布尔表达式视为数字上下文中的整数,“1”为true,“0”为false。