SQL GROUPING SETS

SQL GROUPING SETS

 

SQL GROUPING SETS

SQL GROUPING SETS

The GROUPING SETS clause in SQL allows you to create multiple groupings in a single query, making it easier to generate grouped aggregations for different dimensions. It is part of advanced GROUP BY functionality and is especially useful for generating subtotal and grand total reports.


Why Use GROUPING SETS?

  • To define multiple groupings in a single query without writing multiple GROUP BY queries.
  • To avoid redundancy and improve query efficiency.
  • To create more detailed reports by including subtotals and totals for specific groups.

Syntax

SELECT column1, column2, aggregate_function(column3) FROM table_name GROUP BY GROUPING SETS ( (column1, column2), (column1), (column2), () );

Key Points

  1. Each grouping set defines a specific grouping.
  2. An empty () in GROUPING SETS calculates a grand total.
  3. You can combine it with other advanced features like ROLLUP or CUBE for more detailed analysis.

Example Dataset

Sales Table

RegionProductSales
NorthA100
NorthB200
SouthA150
SouthB250

Examples

1. Using GROUPING SETS for Specific Grouping

SELECT Region, Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY GROUPING SETS ( (Region, Product), (Region), (Product), () );

Result:

RegionProductTotalSales
NorthA100
NorthB200
SouthA150
SouthB250
NorthNULL300
SouthNULL400
NULLA250
NULLB450
NULLNULL700

2. Adding a GROUPING Function

The the GROUPING() function helps identify rows resulting from subtotals or grand totals.

SELECT Region, Product, SUM(Sales) AS TotalSales, GROUPING(Region) AS IsRegionGrouped, GROUPING(Product) AS IsProductGrouped FROM Sales GROUP BY GROUPING SETS ( (Region, Product), (Region), (Product), () );

Result:

RegionProductTotalSalesIsRegionGroupedIsProductGrouped
NorthA10000
NorthB20000
SouthA15000
SouthB25000
NorthNULL30001
SouthNULL40001
NULLA25010
NULLB45010
NULLNULL70011

3. Equivalent Queries Without GROUPING SETS

Without GROUPING SETS, you would need multiple queries combined with UNION ALL.

-- Individual Region and Product SELECT Region, Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY Region, Product UNION ALL -- Subtotal by Region SELECT Region, NULL AS Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY Region UNION ALL -- Subtotal by Product SELECT NULL AS Region, Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY Product UNION ALL -- Grand Total SELECT NULL AS Region, NULL AS Product, SUM(Sales) AS TotalSales FROM Sales;

Using GROUPING SETS simplifies the query and improves maintainability.

4. Combining GROUPING SETS with HAVING

Filter results to exclude certain subtotals.

SELECT Region, Product, SUM(Sales) AS TotalSales FROM Sales GROUP BY GROUPING SETS ( (Region, Product), (Region), () ) HAVING SUM(Sales) > 200;

Result:

RegionProductTotalSales
SouthB250
NorthNULL300
SouthNULL400
NULLNULL700

When to Use GROUPING SETS

  1. Reporting and Analysis:
    • Sales reports showing subtotals by region, product, and overall totals.
  2. Data Warehousing:
    • Advanced aggregation for OLAP (Online Analytical Processing).
  3. Simplifying Queries:
    • Replace multiple GROUP BY queries combined with UNION ALL.

Performance Considerations

  • Indexing: Use appropriate indexes to improve query performance, especially for large datasets.
  • Optimization: Avoid redundant grouping levels to minimize computation time.
  • Database Support: Supported in most major databases, including MySQL (v8.0+), PostgreSQL, SQL Server, and Oracle.

Conclusion

The GROUPING SETS clause is a powerful tool for advanced SQL aggregation, simplifying complex reporting queries by consolidating multiple groupings into one. It improves efficiency, readability, and flexibility in generating detailed reports.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close