Aggregate functions
Creating multilevel totals with aggregate functions might not be the first thing you think about. This has been a capability of the aggregate functionality for quite some time.
The purpose of the GROUP BY
clause is to group rows together, based on the columns specified. But with aggregates, you don't always need to specify columns. When you want a grand total, you can omit the GROUP BY
clause altogether:
SQL> select sum (sal) 2 from emp 3 / SUM(SAL) ---------- 29025
Omitting the GROUP BY
clause leads to a grand total, but you can also use an empty set in the GROUP BY
clause:
SQL> select sum (sal) 2 from emp 3 group by () 4 / SUM(SAL) ---------- 29025
On line 3 the empty set is used, denoted by the opening and closing braces ()
.
There is a lot more about aggregates, such as GROUPING SETS
, ROLLUP
, and CUBE
.
Grouping sets
In the preceding example, we created a grand total by using an empty set in the GROUP BY
clause. The GROUPING SETS
clause...