本文共 436 字,大约阅读时间需要 1 分钟。
文章来源:http://database.51cto.com/art/201009/227645_2.htm
由于要统计合计以及小计,不能简单的用nvl来产生"合计"了,要用grouping函数,来判断者某行是否有rollup产生的合计行,
select case when grouping(emp_id)=1 and grouping(product_id)=1 then '合计' else emp_id end emp_id, case when grouping(emp_id)=0 and grouping(product_id)=1 then '小计' else procudt_id end product_id, sum(qty) qty from t_test group by rollup(emp_id,product_id)注意,grouping(emp_id)=1,说明是有rollup函数生成的行,0为数据库本身有的行。