PostgreSQL – GROUPING SETS

The GROUPING SETS operation is used in conjunction with the GROUP BY clause to generate a result set that includes aggregated data for multiple specified groups of columns. It’s a way to efficiently achieve the same effect as using multiple GROUP BY clauses separately.

The basic syntax of using GROUPING SETS in a query is as follows:

SELECT column_list, aggregate_function(column) FROM table_name GROUP BY GROUPING SETS ((column1, column2, ...), (column1, ...), ...);

Here’s a breakdown of the components:

  • SELECT column_list: This is the list of columns you want to retrieve in the result set. It can include both the columns you are using for grouping and the columns for which you want to apply aggregate functions.
  • aggregate_function(column): This is an aggregate function (like SUM, AVG, COUNT, etc.) that you want to apply to the specified column.
  • table_name: The name of the table from which you want to retrieve data.
  • GROUP BY GROUPING SETS ((column1, column2, ...), (column1, ...), ...): This is where you specify the different sets of columns that you want to use for grouping using the GROUPING SETS operation. The query will generate aggregated data for each set of columns.

Example: Consider a sales table with columns: “product”, “category”, “region”, and “amount”. To obtain aggregated data for total sales amount for various combinations of “product,” “category,” and “region” , you can use the GROUPING SETS operation as follows:

SELECT product, category, region, SUM(amount) AS total_sales FROM sales GROUP BY GROUPING SETS ((product, category, region), (product, category), (product), ());

In this example, the query will generate a result set that includes aggregated sales data for each specified grouping set. It will provide total sales for each individual level of the hierarchy, as well as subtotals for various combinations of columns.

GROUPING SETS is particularly useful when you want to retrieve summary data for specific combinations of columns and levels of aggregation. It’s more flexible and concise compared to using separate GROUP BY clauses for each combination you want to analyze.

Similar Posts