• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query

April 30, 2023 by Andy Hayes Leave a Comment

SQL GROUPING SETS is a feature of the SQL language that allows you to specify multiple group by sets in a single query.

The best way to demonstrate this is with code and examples.

So imagine you have table of sales data which contained the following fields:

  • product
  • region
  • sale_amount

A visual of this would look like:

productregionsale_amount
shoesAlaska1500
shoesTexas2200
shoesWashington6000
pantsAlaska500
pantsTexas600
pantsWashington750
coatsAlaska1725
coatsTexas750
coatsWashington1250

Now say you had a requirement to query this table and aggregate the results. To aggregate data using SQL we use GROUP BY which allows us to group data by one or many fields in our data set and then perform aggregate functions such as SUM(), COUNT(), MIN(), MAX() etc amongst others.

Let’s now assume that you had multiple group by requirements based on the available data. So this would be grouping by the available dimension fields, product and/or region.

Fine so, you write your group by queries like this:

SELECT product, SUM(sale_amount) AS sale_amount

FROM sales

GROUP BY product;

Then you write:

SELECT region, SUM(sale_amount) AS sale_amount

FROM sales

GROUP BY region;

Then you write:

SELECT product, region, SUM(sale_amount) AS sale_amount

FROM sales

GROUP BY product, region;

That’s three queries – three hits to the database to aggregate this data and if you want to put the results together, you have to use UNION ALL.

No no no, there is a better way 🙂

Use SQL GROUPING SETS and you can do this in a single query:

SELECT product, region, sum(sale_amount) as sale_amount

FROM sales

GROUP BY GROUPING SETS ((product), (region), (product, region))

sql grouping sets

This is a really nice and efficient way to aggregate data into multiple groups using a single SQL query. In the returned results, we can see that we have rows based on our GROUPING SETS requirements of GROUP BY product (rows 10-12), GROUP BY region (rows 13-15) and GROUP BY product, region (rows 1-9).

GROUPING SETS is available in database products SQL Server, PostgresSQL and Oracle. MySQL does it a bit differently using WITH ROLLUP.

I’ve written about this in the past looking at performance comparisons of GROUPING SETS v UNION ALL. You can read that one here.

Hope you like the post and if you did, I would love it if you could share it on social media or backlink to it. It would really help to grow this blog. Thank you.

Filed Under: All Articles, SQL Tips and Tricks Tagged With: postgresql, sql, sql server

About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on Google+

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©