0

我需要从许多 mysql 5.6 表中获取数据来创建饼图。如您所知,如果饼图表示有意义的数据,它就很有用。但是,当您有许多无意义的数据点时,例如小于 .. 或不重要的值,饼图就会变得不清楚。我需要计算每个类别的出现次数并将小于 X 的不重要计数汇总为 OTHERS。

目前我做一个

Select category, count(*) as total from table_name group by category.  

它给了我每个类别,而且很重要。如何获得总数超过 50 的类别和低于 50 的类别,在“其他”下进行汇总。谢谢,豪尔赫。

4

2 回答 2

1
SELECT IF(total > 50, category, 'Others') AS category, SUM(total) AS total
FROM (SELECT category, COUNT(*) AS total
      FROM table_name
      GROUP BY category) AS subquery
GROUP BY category
于 2015-05-06T20:58:19.167 回答
0

假设您要汇总少于 4 个条目的所有类别:

select category, count(*) as total from table_name group by category having count(*) >= 4

union

select 'others', sum(c) as total from (
  select category, count(*) c from table_name group by category having count(*) < 4
) tmp
于 2015-05-06T21:09:47.323 回答