在Oracle 11g中,我有一个名为ITEM
的表,其中每条记录分为maingroup
和subgroup
,如下所示:
+-------+---------+----------+
item_id maingroup subgroup
+-------+---------+---------+
1 group1 subgroup1
2 group1 subgroup2
3 group2 subgroup1
4 group2 subgroup2
...
我必须编写一个程序来报告表ITEM
中的项目数。报告输出与此类似:
subgroup1 subgroup2 group_total
group1 10 5 15
group2 0 1 1
subgroup_total 10 6 16
为此,我将编写一个SQL来查询数据,然后使用Java重新格式化输出。 SQL应该自己生成完整的报告,即我将仅使用Java重新格式化输出而不进行任何计算。所以,我决定SQL的输出应该是这样的:
+--------------+-----------+-----+
maingroup subgroup cnt
+--------------+-----------+-----+
group1 subgroup1 10
group1 subgroup2 5
group1 group_total 15
group2 subgroup1 0
group2 subgroup2 1
group2 group_total 1
subgroup_total subgroup1 10
subgroup_total subgroup2 6
subgroup_total group_total 16
理想情况下,SQL就像
一样简单select maingroup, subgroup, count(*) cnt from ITEM
group by maingroup, subgroup union all
select maingroup, 'group_total' as subgroup, count(*) cnt from ITEM
group by maingroup union all
select 'subgroup_total' as maingroup, subgroup, count(*) cnt from ITEM
group by subgroup;
但不,我简化了表格ITEM
使问题易于理解,实际上,为了弥补这个ITEM
,我必须使用一个大的子查询,所以它实际上是看起来像
select maingroup, subgroup, count(*) cnt from
(select maingroup, subgroup from ...) ITEM
group by maingroup, subgroup union all
select maingroup, 'group_total' as subgroup, count(*) cnt from
(select maingroup, subgroup from ...) ITEM
group by maingroup union all
select 'subgroup_total' as maingroup, subgroup, count(*) cnt from
(select maingroup, subgroup from ...) ITEM
group by subgroup;
使用相同的大子查询3次。我希望我可以重用此子查询,但我没有权限创建表或使用WITH子句。 是否可以重新排列上述SQL,以便子查询ITEM
只处理一次而不创建临时表或使用WITH子句?
答案 0 :(得分:5)
尝试使用grouping sets
子句进行查询:
SELECT coalesce( maingroup, 'subgroup_total' ) As maingroup,
coalesce( subgroup, 'group_total' ) As subgroup,
count(*) cnt
FROM very_complex_subquery
GROUP BY GROUPING SETS((maingroup, subgroup), (maingroup), (subgroup))
order by 1,2
;