Multiple columns

SELECT column1, column2, aggregate_function(column)

FROM table

GROUP BY ROLLUP (column1, column2);

Applying Rollup in the above code for multiple columns.

SELECT COALESCE(Department ,’ Total’)as Department ,gender, SUM(Salary) as Salary from Employee1 Group By ROLLUP(Department , Gender)

Output:

Multiple Column

How it Works

Multiple Rollup min

Rollup in SQL Server

The ROLLUP operator enhances the capabilities of the GROUP BY clause by enabling the computation of subtotals and grand totals for a set of columns. It produces a result set that incorporates rows at various levels of aggregation. ROLLUP streamlines the aggregation process by eliminating the need for separate queries to obtain subtotals and totals, resulting in a more streamlined and efficient approach. It is a powerful extension of the GROUP BY clause, enabling users to generate summary reports effortlessly.

Syntax:

SELECT column, aggregate_function(column)

FROM table

GROUP BY ROLLUP (column);

In this syntax:

  • ‘column’ represents the column by which you wish to group your data.
  • ‘aggregate_function(column)’ is an optional step, allowing you to specify an aggregation function (e.g., SUM, COUNT, AVG) for performing calculations on the grouped data.
  • ‘table’ designates the table being queried.
  • ‘GROUP BY’ is a clause used to specify a single column or multiple columns to create a group on which the aggregate operation is performed.
  • ‘ROLLUP’ is used with the combination of the GROUP BY clause for creating multiple groups (i.e., grouping set) and hierarchically applies the aggregate function.

Similar Reads

Need For Rollup Operator

The GROUP BY clause in SQL is used to group rows based on one or more columns. When using GROUP BY, you aggregate data within each group using aggregate functions like SUM, COUNT, AVG, and more. It is an essential component for performing simple aggregations and summarizing data....

Why SQL ROLLUP is Essential?

Multi-Level Aggregation: SQL ROLLUP empowers users to craft summary reports encompassing subtotals and grand totals at various levels of aggregation. This feature is particularly advantageous for financial, business, and operational analyses. Enhanced Efficiency: By eliminating the necessity for multiple queries or intricate manual calculations, SQL ROLLUP significantly enhances the efficiency of data analysis. This is particularly pertinent when dealing with sizable datasets. Hierarchical Data Arrangement: SQL ROLLUP naturally organizes data hierarchically, enabling users to visualize aggregated information at different levels. This characteristic is indispensable for comprehending complex data relationships. Customized Aggregation: SQL ROLLUP allows the application of a variety of aggregation functions to the grouped data, granting users the flexibility to adapt their summarization and analysis strategies to the specific needs of their projects....

1. Single Column

SELECT column, aggregate_function(column) FROM table GROUP BY ROLLUP (column);...

2. Multiple columns

SELECT column1, column2, aggregate_function(column) FROM table GROUP BY ROLLUP (column1, column2);...

Characteristics of Rollup

SQL’s ROLLUP function streamlines multi-level data analysis using a single query. As an extension of GROUP BY, ROLLUP enables aggregations across various dimensions and hierarchical levels within a single SQL query. Results obtained with SQL ROLLUP are unsorted; an ORDER BY clause is necessary to arrange the data in a desired order....

Conclusion

“Rollup, typically employed for reporting tasks, operates as a subclause within the GROUP BY statement. It inherently assumes a hierarchy among the columns, allowing for the generation of subtotals in a structured manner. The Rollup feature adopts a hierarchical perspective, effectively organizing data into different levels for a comprehensive view of the information.”...