Oracle Group By Grouping Sets

Your rating: None Average: 2.5 (4 votes)

In one of my previous ROLLUP posts I showed you how to generate summary information. However, the output of ROLLUP includes the rows produced by the regular GROUP BY operation along with the summary rows.

So, what if you are only interested in totals. Well, Oracle does provide an extension to the GROUP BY clause called GROUPING SETS that you can use to generate summary information at the level you choose without including all the rows produced by the regular GROUP BY operation.

Lets take a look at an example:

SELECT ord.year,
   TO_CHAR(TO_DATE(ord.month, 'MM'), 'Month') month,
   reg.region,
  SUM(ord.total_sales)
FROM orders ord, region reg
WHERE ord.region_id = reg.region_id
AND ord.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (ord.year, ord.month, reg.region);

      YEAR MONTH     REGION               SUM(O.TOT_SALES)

---------- --------- -------------------- ----------------

                     Mid-Atlantic                  5029212

                     New England                   5074332

                     Southeast US                  4960311

           January                                 4496799

           February                                4988535

           March                                   5578521

      2000                                        10042570

      2001                                         5021285



8 rows selected.

As you can see, the output above contains ONLY the subtotals at region, month and year levels.

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.
Guest's picture
Guest (not verified)
Re: Oracle Group By Grouping Sets

And what if I'm interested in totals but I want only one row for different years?

for example:

YEAR JAN FEB MAR
------ ----------- ----------- ------------
2000 4496799 4988535 5578521
2001 5029212 5074332 4960311

Lepa's picture
User offline. Last seen 1 year 3 days ago. Offline
Joined: 06/23/2008
Posts: 591
Re: Oracle Group By Grouping Sets

>> but I want only one row for different years
You can get the total for each month in every year by grouping by year and month. But you will not have one and only one row for each year. To accomplish what you want, you need to look at how to convert rows into columns. Here is a tip on how to do that posted at Oracle's site
http://www.oracle.com/technology/oramag/code/tips2004/050304.html

Give back to the community and help it grow!
* Help with unanswered forum questions and issues
* Register or login to share your knowledge at your own blog

Guest's picture
Guest (not verified)
Re: Oracle Group By Grouping Sets

select the employee using group by function where department member is greater than 4

Plz help me on the matter

Guest's picture
Guest (not verified)
Re: Oracle Group By Grouping Sets

Hello Lepa,

I have been looking for the sample database that contains the orders and region tables you used in this example without success.

I am using 11gr2 and I believe I installed all the sample schemas. Where would I find the tables/data you've been using in your examples ?

Thank you,

John.