转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/9674443
我们都知道group by能用来分组的子句;扩展了的group by加上rollup子句,就能为每一个分组返回一条小计录,并为全部分组返回总记录,操作如下:
gyj@OCM> WITH t AS 2 (SELECT 'A' comp,'x' cate,'o' oth,100 fee FROM dual UNION ALL 3 SELECT 'A' comp,'y' cate,'m' oth,200 fee FROM dual UNION ALL 4 SELECT 'B' comp,'x' cate,'o' oth,300 fee FROM dual UNION ALL 5 SELECT 'B' comp,'y' cate,'m' oth,100 fee FROM dual) 6 SELECT comp,cate,oth,SUM(fee) 7 FROM t 8 GROUP BY comp,ROLLUP((cate,oth)); C C O SUM(FEE) - - - ---------- A x o 100 A y m 200 A 300 B x o 300 B y m 100 B 400 6 rows selected.
答案A是说只找每行小,这个应该是grouping sets,操作如下:
gyj@OCM> WITH t AS 2 (SELECT 'A' comp,'x' cate,'o' oth,100 fee FROM dual UNION ALL 3 SELECT 'A' comp,'y' cate,'m' oth,200 fee FROM dual UNION ALL 4 SELECT 'B' comp,'x' cate,'o' oth,300 fee FROM dual UNION ALL 5 SELECT 'B' comp,'y' cate,'m' oth,100 fee FROM dual) 6 SELECT comp,cate,oth,SUM(fee) 7 FROM t 8 GROUP BY comp,GROUPING SETS((cate,oth)); C C O SUM(FEE) - - - ---------- A y m 200 B x o 300 B y m 100 A x o 100
答案B是说每组的合计,肯定不对了。
答案C看到一个right toleft(从右到左),因为rollup(a,b,c)是按照这顺序的先group by a,b,c然后a,b,然后a,然后不分组,从右到左4个结果unionall。
答案D是说组合所有可能,那应该是CUBE。
gyj@OCM> WITH t AS 2 (SELECT 1 a,2 b,3 c,4 d,5 e,6 f,7g,8 h 3 FROM dual) 4 SELECT 5 decode(GROUPING(a),1,0,1) a, 6 decode(GROUPING(b),1,0,1) b, 7 decode(GROUPING(c),1,0,1) c, 8 decode(GROUPING(d),1,0,1) d, 9 decode(GROUPING(e),1,0,1) e, 10 decode(GROUPING(f),1,0,1) f, 11 decode(GROUPING(g),1,0,1) g, 12 decode(GROUPING(h),1,0,1) h 13 FROM t 14 GROUP BY CUBE(a,b,c,d,e,f,g,h); A B C D E F G H ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 省略结果。。。。。。 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 256 rows selected.
详细可以买本“剑破冰山---oracle开发”那书看看。
正确答案是:C
QQ:252803295
学习交流QQ群:
DSI&Core Search Ⅰ 群:127149411(技术:已满)
DSI&Core Search Ⅱ 群:177089463(技术:未满)
DSI&Core Search Ⅲ 群:284596437(技术:未满)
DSI&Core Search Ⅳ 群:192136702(技术:未满)
DSI&Core Search Ⅴ 群:285030382(闲聊:未满)
MAIL:oracledba_cn@hotmail.com
BLOG: http://blog.csdn.net/guoyjoe
WEIBO:http://weibo.com/guoyJoe0218
ITPUB: http://www.itpub.net/space-uid-28460966.html
OCM: http://education.oracle.com/education/otn/YGuo.HTM