日期:2014-05-16 浏览次数:20545 次
SQL> select grade,id,num from a; GRADE ID NUM ---------- ---------- ---------- a 1 1 a 2 2 b 3 4 b 4 4
SQL> select grade,sum(num) from a group by rollup(grade); GRADE SUM(NUM) ---------- ---------- a 3 b 8 11
SQL> SELECT decode(grouping_id(grade,ID),2,'小计',3,'合计',grade) grade, 2 decode(grouping_id(grade,ID),1,'小计',3,'合计',ID) ID, 3 SUM(num) 4 FROM a GROUP BY ROLLUP(grade,ID) 5 /
GRADE ID SUM(NUM) ---------- ---------- ---------- a 1 1 a 2 2 a 小计 3 b 3 4 b 4 4 b 小计 8 合计 合计 11 7 rows selected
SQL> SELECT grade, 2 decode(GROUPING(ID),1,'合计',ID) ID, 3 SUM(num) 4 FROM a GROUP BY grade,rollup(ID) 5 / GRADE ID SUM(NUM) ---------- ---------- ---------- a 1 1 a 2 2 a 合计 3 b 3 4 b 4 4 b 合计 8 6 rows selected
SQL> select part,grade,id,num from a; PART GRADE ID NUM ---- ---------- ---------- ---------- p1 a 1 1 p1 a 2 2 p1 b 3 3 p1 b 4 4 p2 c 5 5 p2 d 6