/* ORACLE or PostgreSQL - Using ROLLUP() */

select vendor, sum(amount), count(amount)
from payment
group by ROLLUP(vendor);



/* Rollup by vendor and description */

select vendor, description type, sum(amount), count(amount)
from payment
group by ROLLUP(vendor, description);



/* Doing the same, but using GROUPING SETS instead*/

select vendor, description type, sum(amount), count(amount)
from payment
group by GROUPING SETS( (vendor, description), (vendor), () );



/* Multi-dimentional CUBE rollup  */

select vendor, description type, sum(amount), count(amount)
from payment
group by CUBE(vendor, description)
order by vendor, description;



/* Displaying 'total' for all rolled up lines  */
/* Give vendor or =TOTAL= when vendor is null  */

select 
    COALESCE(vendor,      '=TOTAL=')  as vendor,
    COALESCE(description, '-total-')  as description,
    count(amount), sum(amount)
from payment
group by ROLLUP(vendor, description);