SQL – making several groupings (performance)

I have some SQL query that founds records based on provided parameters. That query is pretty heavy, so I want to execute it less as possible.

After I getting result from that query, I need to perform its breakdown.

For example, consider the following query:

SELECT location, department, industry
FROM data

After that, I need to perform breakdown of that results, e.g. I need to provide list of all locations where from I have results and counts of each type, same for departments and same for industries.

As I know, in order to get breakdown by locations, I need to perform GROUP BY (location) and then count.

My question is: is it possible, for performance considerations, to perform several groupings/ counts on query result without recalculating it over and over again for each grouping?

Source: sql

Leave a Reply