A database system computes an aggregate on distinct values of at least one attribute for each of plural groups specified by multiple levels of grouping sets. Distinct values of the at least one attribute are identified and stored in a first table. The first table is used to compute aggregates for groups specified by a first grouping set and for groups specified by a second grouping set.