Skip to main content

ROLLUP IN ORACLE

ROLLUP
=======

Extension of Group By Clause, which Provide super aggregate rows.

Ex:-
-----
Query for sum of salaries by department and also display the total salary of all the departments.

Using Set Operator
-------------------

SQL> SELECT DEPTNO,SUM(SAL)
2 FROM
3 EMP GROUP BY DEPTNO
4 UNION
5 SELECT NULL,SUM(SAL)
6 FROM
7 EMP;

DEPTNO SUM(SAL)
---------- ----------
10 7450
20 10875
30 9400
50 1300
29025

Using Inline Views
--------------------
SQL> SELECT DEPTNO,SUM(SAL) DEPTSAL,A.TOTALSAL TOTALSALARY
2 FROM
3 EMP,(SELECT SUM(SAL) TOTALSAL
4 FROM
5 EMP) A
6 GROUP BY DEPTNO,A.TOTALSAL;

DEPTNO DEPTSAL TOTALSALARY
---------- ---------- -----------
10 7450 29025
20 10875
30 9400
50 1300



Using Rollup
-------------
SQL> SELECT DEPTNO,SUM(SAL)
2 FROM
3 EMP
4 GROUP BY ROLLUP(DEPTNO);

DEPTNO SUM(SAL)
---------- ----------
10 7450
20 10875
30 9400
50 1300
29025

SQL> SELECT DECODE(DEPTNO,NULL,'TOTAL_SAL=>',DEPTNO) AS DEPTNO,SUM(SAL)
2 FROM
3 EMP
4 GROUP BY ROLLUP(DEPTNO);

DEPTNO SUM(SAL)
---------------------------------------- ----------
10 7450
20 10875
30 9400
50 1300
TOTAL_SAL=> 29025


Ex:-
----
Query for display sum (salary) year wise as well the total sal.

SQL> SELECT DECODE(TO_CHAR(HIREDATE,'YYYY'),NULL,'TOTAL SALARY =>',TO_CHAR(HIREDATE,'YYYY')) YEAR,SUM(SAL) YEAR_SAL
2 FROM
3 EMP
4 GROUP BY ROLLUP(TO_CHAR(HIREDATE,'YYYY'));

YEAR YEAR_SAL
--------------- ----------
1980 800
1981 22825
1982 1300
1987 4100
TOTAL SALARY => 29025


SQL> BREAK ON TOTALSAL;
SQL> SELECT TO_CHAR(HIREDATE,'YYYY') YEAR,SUM(SAL) YEAR_SAL,A.TOTALSAL
2 FROM
3 EMP,(SELECT SUM(SAL) TOTALSAL
4 FROM
5 EMP) A
6 GROUP BY TO_CHAR(EMP.HIREDATE,'YYYY'),A.TOTALSAL;

YEAR YEAR_SAL TOTALSAL
---- ---------- ----------
1987 4100 29025
1982 1300
1980 800
1981 22825


Ex:-
----
Query for displaying the avg(sal) by dept and job and department avg(sal)

SQL> BREAK ON AVG_SAL_FOR_DEPT_SAL
SQL> SELECT
2 A.DEPTNO,A.JOB,AVG(A.SAL) AVG_SAL_FOR_DEPT_JOB,B.SAL AVG_SAL_FOR_DEPT_SAL
3 FROM
4 EMP A,(SELECT C.DEPTNO,AVG(C.SAL) SAL
5 FROM
6 (
7 SELECT DEPTNO,AVG(SAL) SAL
8 FROM
9 EMP
10 GROUP BY DEPTNO,JOB) C
11 GROUP BY DEPTNO) B
12 WHERE A.DEPTNO=B.DEPTNO
13 GROUP BY A.DEPTNO,A.JOB,B.SAL
14 /

DEPTNO JOB AVG_SAL_FOR_DEPT_JOB AVG_SAL_FOR_DEPT_SAL
---------- --------- -------------------- ----------------
30 SALESMAN 1400 1733.33333
30 MANAGER 2850
30 CLERK 950
20 MANAGER 2975 2308.33333
10 MANAGER 2450 3725
20 ANALYST 3000 2308.33333
20 CLERK 950
50 CLERK 1300 1300
10 PRESIDENT 5000 3725



Using ROLLUP:-
-------------------

SQL> SELECT DEPTNO,JOB,AVG(SAL)
2 FROM
3 EMP
4 GROUP BY ROLLUP(DEPTNO,JOB);

DEPTNO JOB AVG(SAL)
---------- --------- ----------
10 MANAGER 2450 => Select deptno,job,avg(sal) from emp group by deptno,job;
10 PRESIDENT 5000
10 3725
20 CLERK 950
20 ANALYST 3000
20 MANAGER 2975
20 2175
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 1400
30 1566.66667 => Select deptno,avg(sal) from emp group by deptno;
50 CLERK 1300
50 1300
2073.21429 => Select avg(sal) from emp;

However, this is the wrong approach to reach out the above result using Rollup.

Here is the way to go.

SQL> SELECT DEPTNO,JOB,AVG(AVG_SAL)
2 FROM
3 (
4 SELECT DEPTNO,DECODE(JOB,NULL,'SUB',JOB) AS JOB,AVG(SAL) AVG_SAL
5 FROM
6 EMP
7 GROUP BY ROLLUP(DEPTNO,JOB) HAVING JOB!='SUB'
8 )
9 GROUP BY ROLLUP(DEPTNO,JOB);

DEPTNO JOB AVG(AVG_SAL)
---------- --------- ------------
10 MANAGER 2450
10 PRESIDENT 5000
10 3725
20 CLERK 950
20 ANALYST 3000
20 MANAGER 2975
20 2308.33333
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 1400
30 1733.33333
50 CLERK 1300
50 1300
2319.44444


Then what about ROLLUP(DEPTNO),JOB?
----------------------------------------------

SQL> SELECT DEPTNO,JOB,AVG(SAL)
2 FROM
3 EMP
4 GROUP BY ROLLUP(DEPTNO),JOB ORDER BY DEPTNO;

DEPTNO JOB AVG(SAL)
---------- --------- ----------
10 MANAGER 2450 => SELECT DEPTNO,JOB,AVG(SAL) FROM EMP GROUP BY DEPTNO,JOB;
10 PRESIDENT 5000
20 MANAGER 2975
20 CLERK 950
20 ANALYST 3000
30 MANAGER 2850
30 CLERK 950
30 SALESMAN 1400
50 CLERK 1300
CLERK 1037.5 => SELECT JOB,AVG(SAL) FROM EMP GROUP BY JOB;
SALESMAN 1400
PRESIDENT 5000
ANALYST 3000
MANAGER 2758.33333

DEPTNO,ROLLUP(JOB)?
---------------------------

SQL> SELECT DEPTNO,JOB,AVG(SAL)
2 FROM
3 EMP
4 GROUP BY DEPTNO,ROLLUP(JOB) ORDER BY DEPTNO;  Similar to ROLLUP(DEPTNO,JOB)

DEPTNO JOB AVG(SAL)
---------- --------- ----------
10 MANAGER 2450 => SELECT DEPTNO,JOB,AVG(SAL) FROM EMP GROUP BY DEPTNO,JOB;
10 PRESIDENT 5000
10 3725 => SELECT DEPTNO,AVG(SAL) FROM EMP GROUP BY DEPTNO.
20 ANALYST 3000
20 CLERK 950
20 MANAGER 2975
20 2175
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 1400
30 1566.66667
50 CLERK 1300
50 1300

ROLLUP(arg1,arg2,arg3)
-----------------------------

1. Group by arg1,arg2,arg3 and aggregate function
2. Group by arg1,arg2 and aggregate function
3. Group by arg1 and aggregate function
4. Only the aggregate.


Comments

  1. Thiripal: Very good blog.It is very useful for people who want become proficient in oracle and postgreSQL.

    ReplyDelete

Post a Comment

Popular posts from this blog

Pgpool Configuration & Failback

I would like to share the pgpool configuration, and it's failback mechanism in this post.

Hope it will be helpful to you in creating pgpool and it's failback setup.

Pgpool Installation & Configuration

1. Download the pgpool from below link(Latest version is 3.2.1).
    http://www.pgpool.net/mediawiki/index.php/Downloads


2. Untart the pgpool-II-3.2.1.tar.gz and goto pgpool-II-3.2.1 directory.

3. Install the pgpool by executing the below commands:

./configure ­­prefix=/opt/PostgreSQL92/ ­­--with­-pgsql­-includedir=/opt/PostgreSQL92/include/ --with­-pgsql­-libdir=/opt/PostgreSQL92/lib/ make make install 4. You can see the pgpool files in /opt/PostgreSQL92/bin location.
/opt/PostgreSQL92/bin $ ls clusterdb   droplang  pcp_attach_node  pcp_proc_count pcp_systemdb_info  pg_controldata  pgpool pg_test_fsync pltcl_loadmod  reindexdb createdb    dropuser  pcp_detach_node  pcp_proc_info createlang  ecpg      pcp_node_count   pcp_promote_node oid2name  pcp_pool_status  pcp_stop_pgpool  …

pgBucket - A new concurrent job scheduler

Hi All,

I'm so excited to announce about my first contribution tool for postgresql. I have been working with PostgreSQL from 2011 and I'm really impressed with such a nice database.

I started few projects in last 2 years like pgHawk[A beautiful report generator for Openwatch] , pgOwlt [CUI monitoring. It is still under development, incase you are interested to see what it is, attaching the image here for you ],


pgBucket [Which I'm gonna talk about] and learned a lot and lot about PostgreSQL/Linux internals.

Using pgBucket we can schedule jobs easily and we can also maintain them using it's CLI options. We can update/insert/delete jobs at online. And here is its architecture which gives you a basic idea about how it works.


Yeah, I know there are other good job schedulers available for PostgreSQL. I haven't tested them and not comparing them with this, as I implemented it in my way.
Features are: OS/DB jobsCron style sytaxOnline job modificationsRequired cli options

N-Node Mutlimaster Replication With Bucardo...!

Our team recently got  a problem, which is to solve the N-Node multi master replication in PostgreSQL.

We all know that, there are some other db engines like Postgres-XC which works in this way. But, we don't have any tool available in PostgreSQL, except Bucardo.

Bucardo is the nice solution for 2-Nodes. Is there a way we can exceed this limitation from 2 to N..?

As an initial step on this, I have done with 3 Nodes, which I believe, we can extend this upto N. { I might be wrong here.}

Please follow the below steps to set up the 1 - 1 multi master replication.

1. Follow the below steps to get all the pre-requisites for the Bucardo.

yum install perl-DBIx-Safe or apt-get install libdbix-safe-perl Install the below components from CPAN. DBI DBD::Pg Test::Simple boolean (Bucardo 5.0 and higher) Download the latest tarball from here. tar xvfz Bucardo-4.4.8.tar.gz cd Bucardo-4.4.8 perl Makefile.PL make sudo make install 2. We need to create plperl extension in db. For this, download…