Solved

oracle rollup query

Posted on 2016-11-03
3
59 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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.

831 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