引用:http://blog.csdn.net/damenggege123/article/details/38794351
使用里面的数据进行查询来区别rollup()和rollup(())的使用。
select f_workarea, f_line, sum(f_pagesnumber) sum_pagesnumbers
from t_testcount
group by rollup(f_workarea, f_line);
返回结果
F_WORKAREA F_LINE SUM_PAGESNUMBERS
a a1 1
a a2 2
a a3 3
a 6
b b1 1
b 1
7
上面结果存在小计和总计
select f_workarea, f_line, sum(f_pagesnumber) sum_pagesnumbers
from t_testcount
group by rollup((f_workarea, f_line));
结果如下:
F_WORKAREA F_LINE SUM_PAGESNUMBERS
a a1 1
a a2 2
a a3 3
b b1 1
7
上面结果里面明显没有小计,只有总计。
所以rollup()和rollup(())的区别即rollup(())只有总计而没有小计。
如果没有rollup(())怎么实现只有总计没有小计呢。
select *
from (select grouping(f_line) as a1,
grouping(f_workarea) as a2,
f_workarea,
f_line,
sum(f_pagesnumber) sum_pagesnumbers
from t_testcount
group by rollup(f_workarea, f_line)) t
where t.a1 = 0
and t.a2 = 0
or (t.a1 = 1 and t.a2 = 1)
结果如下:
A1 A2 F_WORKAREA F_LINE SUM_PAGESNUMBERS
0 0 a a1 1
0 0 a a2 2
0 0 a a3 3
0 0 b b1 1
1 1 7