说我的表看起来像这样:
现在我可以通过简单的Group By获得a,b的完整总和,但是是否可以通过单个优化查询获得上述嵌套的group-by类型输出?
我现在使用的解决方法是检索Cat1 = a的所有记录,然后在Cat2上应用一个组。但由于Cat1的数量很多(a,b,c,d ......等),因此查询过多。
你会建议什么?
答案 0 :(得分:1)
select cat1,
sum(case when cat2='x' then val end) as sumValForX,
sum(case when cat2='y' then val end) as sumValForY
from your_table
group by cat1