我有一个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语句)以获得所需输出的最有效方法是什么?
答案 0 :(得分:0)
您可以通过对CATEGORY
和SERIES
进行分组来获得所需的结果:
SELECT
CATEGORY, SERIES, COUNT(*)
FROM FRUIT
GROUP BY CATEGORY, SERIES
ORDER BY COUNT(*);
更新:
首先按CATEGORY
总计,然后按绿色,黄色,红色排序,就像您预期的输出一样:
SELECT t1.*
FROM (
SELECT
CATEGORY, SERIES, COUNT(*) AS CNT
FROM FRUIT
GROUP BY CATEGORY, SERIES
) t1
INNER JOIN (
SELECT
CATEGORY, COUNT(*) AS CNT
FROM FRUIT
GROUP BY CATEGORY
) t2
ON t1.CATEGORY = t2.CATEGORY
ORDER BY
t2.CNT DESC,
CASE t1.SERIES
WHEN 'green' THEN 1
WHEN 'yellow' THEN 2
WHEN 'red' THEN 3
END
答案 1 :(得分:0)
一些较短的版本:
select category, series, CntS
from (
select distinct count(category) over (partition by category) cntC,
count(series) over (partition by category, series ) cntS,
category, series
from fruit ) Tab
order by CntC desc, cntS desc;