Home » SQL Server ROLLUP

SQL Server ROLLUP

The ROLLUP clause in SQL Server is an extension of the grouping set operator. This article will give a complete overview of the ROLLUP clause to aggregates different groups with subtotals and grand totals.

It is the subclass of the GROUP BY clause that performs an aggregate operation on multiple levels in a hierarchy and prepares summary reports. It allows us to generate multiple grouping sets within a single query, which is impossible with the GROUP BY clause as it aggregates a single group. Thus, we can say that the ROLLUP provides a more detailed analysis by employing a single query to create several grouping sets along the hierarchy of columns.

NOTE: ROLLUP modifier produces the summary output, including extra rows that represent super-aggregate summary operations. It is mainly used to provide support for OLAP (Online Analytical Processing) operations.

Syntax

The following is the basic syntax that illustrates the ROLLUP clause in SQL Server:

The parameters of the above syntax are described below:

  • column_lists: It indicates the name of columns from the defined table.
  • aggregate_function (column): It is used to perform aggregation on given columns such as SUM, COUNT, AVG, etc.
  • table_name: It indicates the source table name from which data will be fetched.
  • GROUP BY: It’s a clause used to specify a single column or multiple columns to create a group on which the aggregate operation is performed.
  • ROLLUP: It is used with the combination of the GROUP BY clause for creating multiple groups (i.e., grouping set) and hierarchically applies the aggregate function.

SQL Server also provides another syntax to use the ROLLUP clause as given below:

Both the syntax will return the same output.

SQL Server ROLLUP Example

Let us understand when and how the ROLLUP clause is used in the query practically. We can do this by first creating a table named EMPLOYEE using the below statement:

Next, we will insert some records into this table as below:

Execute the SELECT statement to verify the table data:

SQL Server ROLLUP

In the above table, we can see that we have got employees from three different countries US, UK, and India. Now, based on these table data, we want to write a query to retrieve salaries grouped by country as below:

Executing the query will return the following output:

SQL Server ROLLUP

This query is good if we want to get the total salary based on country. But it’s not a better option when we want to get the grand totals of all salaries in the result also because it can only aggregate data at one level, which is subtotal, not grand totals. Hence, we will use the ROLLUP clause along with GROUP BY to examine both the subtotal and the grand totals for a category as it extends the capability of the GROUP BY clause.

The following query is used to retrieve subtotal and grand totals of salaries grouped by country:

We will get the below output:

SQL Server ROLLUP

We can also get the same output using the below syntax:

Here is the result:

SQL Server ROLLUP

In the output, we can see the total salary for each country as well as returns the grand totals of all salaries. The ROLLUP clause will add an extra row in the result set to display the grand totals. Here NULL represents the grand totals. We can also give it a meaningful name using the COALESCE function to replace the NULL value with any provided text.

The below statement will substitute “Grand Total” for the null value:

Here is the result that replaced the NULL value with the given text:

SQL Server ROLLUP

ROLLUP with multiple columns

We already know that the ROLLUP clause performs aggregate operations on multiple levels in a hierarchy and prepare summary reports at each level within a single query.

Let’s understand this concept with an example. Suppose there are multiple columns in the GROUP BY clause. In this case, the ROLLUP clause assumes a hierarchy among the columns supplied in the GROUP BY clause. The query adds an extra super-aggregate summary row to the end of the result whenever a column value changes.

For example, we have specified the three columns in the GROUP BY clause as below:

The ROLLUP modifier assumes the hierarchy as below:

And generates the following grouping set:

The following query helps to understand it more clearly:

Since we have not used the ROLLUP clause, a summary of the employee table based on multiple columns supplied in the GROUP BY clause would look like the below output. In this case, we will only receive summary values at the gender/country level of analysis.

SQL Server ROLLUP

Now we will analyze the output with the ROLLUP clause:

Executing the query will display the following output where we can see the several extra rows:

SQL Server ROLLUP

This output generates data at three different levels of analysis, which are described below:

  • The first level generates an extra sub-aggregate summary row for each set of female employees and then displays the total salary for all employees. It will set the country column to NULL.
  • The second level generates an extra sub-aggregate summary row for each set of male employees and then displays the total salary. It will also set the country column to NULL.
  • Finally, the third level will generate an extra super-aggregate summary row that shows the grand totals for all columns. It will set the gender and country columns to NULL.

It ensures that we get a different answer if we modify the order of the columns defined in the GROUP BY clause. See the below query:

Here is the result:

SQL Server ROLLUP

GROUPING Function with ROLLUP

The GROUPING() function determines whether NULL in the output is a regular grouped value, a super-aggregate value, or grand totals. It yields one when NULL occurs in a supper-aggregate row. Otherwise, it returns 0. The GROUPING() function can be used in the select list, HAVING clause, and ORDER BY clause.

See the below query:

Execute the query will return the below output:

SQL Server ROLLUP

In the output, we can see that the GROUPING(gender) returns one when NULL in the gender column occurs in a super-aggregate row. Otherwise, it will return zero.

Similarly, the GROUPING(country) returns one when NULL in the country column occurs in a super-aggregate row. Otherwise, it will return zero.

How is ROLLUP different from CUBE?

ROLLUP and CUBE in SQL Server are used for reporting purposes and display the subtotal and grand totals. We generally use these clauses with the GROUP BY clause. However, both are used for similar objectives, but they have some differences also.

ROLLUP returns an output that shows aggregates for a hierarchy of values in the columns you’ve chosen. On the other hand, CUBE creates a result set that includes aggregates for all possible combinations of values in the columns that have been selected.

Let us take an example to illustrate these differences. Suppose we have three columns c1, c2, and c3. Next, we will perform aggregation at a different level.

If we use these columns with the CUBE clause as CUBE(c1, c2, c3), we will get the eight possible grouping sets:

If we use these columns with the ROLLUP clause as ROLLUP(c1, c2, c3), we will get only four possible grouping sets:


You may also like