Solved

oracle rollup query

Posted on 2016-11-03
3
65 Views
Last Modified: 2016-11-04
Hi ,

My question might sound easy , but I want to understand why oracle is not generating a grand total when I fire the below query

Select department_id , job_id , count(*)
from employees
group by department_id,rollup(department_id, job_id)

I get the grandtotal with rollup , if i fire the below query

Select department_id , job_id , count(*)
from employees
group by rollup(department_id, job_id)

Any help is really appreciated. The above queries are firedin HR schema.
0
Comment
Question by:sam_2012
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 41872834
rollup()   applies the aggregates within the grouping.

In the first query you have grouped within department, so the rollup only generates counts within department.  The total within a department across all departments is still just the department.

That's why each department has 2 rows with a null job_id.   The second row is the grand total.

In the second query, you don't have a grouping other than the rollup itself so it generates totals on departments and across all departments.
1
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41873693
well, in query1 you first group by department_id
that has priority over the rollup

that's why you don't get a total
0
 

Author Closing Comment

by:sam_2012
ID: 41874822
awesome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle sql query 7 75
exp/imp 25 87
How to Comment Out Lines of Code in a Pass Through Query In MS Access 2016 19 64
Oracle dataguard 5 47
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question