如何通过列值计数来计算百分比

时间:2019-05-06 14:20:03

标签: mysql sql

给出这样的表。

+----+-------+-----+
| id | class | tag |
+----+-------+-----+
|  1 |     1 | aaa |
|  2 |     1 | abb |
|  3 |     1 | acc |
|  4 |     1 | baa |
|  5 |     2 | bbb |
|  6 |     2 | aaa |
+----+-------+-----+

对于表中的每个类,如何计算同一类中以a开头的标签的数量?上表的结果将是:

+-------+---------+
| class | percent |
+-------+---------+
|     1 |  0.7500 |
|     2 |  0.5000 |
+-------+---------+

PS:我已经有了一个解决方案,但是表很大(几百万行),您可以优化吗?

SELECT class, (CAST(cnt_1 AS DECIMAL) / cnt_2) AS percent 
FROM (
  SELECT class, count(tag) AS cnt_1 FROM (
    SELECT class, tag FROM t_test 
    WHERE tag LIKE 'a%'
  ) AS tmp1 
  GROUP BY class
) AS tmp2 
NATURAL JOIN (
  SELECT class, count(tag) AS cnt_2 
  FROM t_test GROUP BY class
) AS tmp3;

2 个答案:

答案 0 :(得分:1)

我将使用条件聚合:

select class, avg( tag like 'a%' ) as a_ratio
from t_test
group by class;

百分比通常从0到100。ratio是您的列的更好名称。

Here是db <>小提琴,它说明了此方法。

答案 1 :(得分:0)

更新:偶然地,这与Gordon的答案非常相似。我只是添加一个修改,以使“百分比”看起来像百分比

select class, round((avg(tag like 'a%')*100),2) percent
from t_test
group by class;