我有一个 Oracle 表,其中包含类似于以下基本示例的数据:
+--------+----------+
| SERIES | CATEGORY |
+--------+----------+
| green | apple |
| green | pear |
| green | pear |
| yellow | apple |
| yellow | apple |
| yellow | pear |
| yellow | pear |
| yellow | pear |
| yellow | banana |
| yellow | banana |
| yellow | banana |
| red | apple |
+--------+----------+
我想为这些数据生成一个类似于 Pareto 的图表,它应该看起来像 Stacked Pareto Chart,
要创建此图,我想运行 SQL 查询并获得以下输出:
+----------+--------+-------+
| CATEGORY | SERIES | COUNT |
+----------+--------+-------+
| pear | green | 2 |
| pear | yellow | 3 |
| apple | green | 1 |
| apple | yellow | 2 |
| apple | red | 1 |
| banana | yellow | 3 |
+----------+--------+-------+
实际表有数百万个条目,目前查询数据库需要大量时间,因为我正在使用的当前过程效率不高:
按每个类别中的条目数量对类别进行排序:
SELECT CATEGORY, COUNT(CATEGORY) FROM FRUIT GROUP BY CATEGORY ORDER BY COUNT(CATEGORY);
然后对于每个类别,我按系列的顺序列出相关系列:
SELECT SERIES, COUNT(SERIES) FROM FRUIT WHERE CATEGORY = [current category] GROUP BY SERIES ORDER BY SERIES;
查询数据库(最好是单个 SQL 语句)以获得所需输出的最有效方法是什么?