Home » SQL & PL/SQL » SQL & PL/SQL » Hierarchical queries with aggregation (11.2.0.3)
Hierarchical queries with aggregation [message #636311] Tue, 21 April 2015 05:42 Go to next message
grpatwari
Messages: 288
Registered: June 2008
Location: Hyderabad
Senior Member
Hi,

I have one requirement in my assignment.


create table empl(Empno	number, Ename	varchar2(100),Mgrno number,Sal	number(7,2),A_Sal number(9,2));

INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (1,'KING', NULL, 10000, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (2,'BLAKE',1, 1000, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (3,'ALLEN', 2, 100, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (4,'JAMES', 2, 200, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (5,'MARTIN', 2, 300, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (6,'TURNER', 2, 400, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (7,'WARD', 2, 500, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (8,'CLARK', 1, 2000, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (9,'MILLER', 8, 600, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (10,'JONES', 1, 3000, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (11,'FORD', 10, 700, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (12,'SMITH', 11, 111, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (13,'SCOTT', 10, 800, NULL);
INSERT INTO empl (Empno, Ename, Mgrno, Sal, A_Sal) VALUES (14,'ADAMS', 13, 222, NULL);

KING
   BLAKE
      ALLEN
      JAMES
      MARTIN
      TURNER
      WARD
   CLARK
      MILLER
   JONES
      FORD
         SMITH
      SCOTT
         ADAMS


KING is parent of BLAKE, CLARK and JONES
BLAKE is parent of ALLEN, JAMES, MARTIN, TURNER and WARD
CLARK is parent of MILLER
JONES is parent of FORD and SCOTT
FORD is parent of SMITH
SCOTT is parent of ADAMS

I want to update A_SAL column in the table based on parent.

For example:
ADAMS A_sal = ADAMS sal (becoz ADAMS doesn't have any child)
SCOTT A_sal = ADAMS sal (or) ADAMS A_sal + SCOTT sal

SMITH A_sal = SMITH sal (becoz SMITH doesn't have any child)
FORD A_sal = SMITH sal (or) SMITH A_sal + FORD sal

JONES A_sal = FORD sal (or) FORD A_sal + SCOTT sal (or) SCOTT A_sal

MILLER A_sal = MILLER sal (becoz MILLER doesn't have any child)
CLARK A_sal = MILLER sal (or) MILLER A_sal + CLARK sal

ALLEN A_sal = ALLEN sal (becoz ALLEN doesn't have any child)
JAMES A_sal = JAMES sal (becoz JAMES doesn't have any child)
MARTIN A_sal = MARTIN sal (becoz MARTIN doesn't have any child)
TURNER A_sal = TURNER sal (becoz TURNER doesn't have any child)
WARD A_sal = WARD sal (becoz WARD doesn't have any child)

BLAKE A_SAL = ALLEN sal (or) ALLEN A_sal+ JAMES sal (or) JAMES A_sal+ MARTIN sal (or) MARTIN A_sal + TURNER sal (or) TURNER A_sal + WARD sal (or) WARD A_sal + BLAKE SAL

KING A_SAL = BLAKE sal (or) BLAKE A_SAL + CLARK sal (or) CLARK A_sal+ JONES sal (or) JONES A_sal

I have written the procedure to update from child records A_SAl to parent records of A_sal.

Please let me know how can I update by using hierarchical queries or is there any way to do this. Please suggest on this.
Re: Hierarchical queries with aggregation [message #636312 is a reply to message #636311] Tue, 21 April 2015 05:56 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Quote:
SCOTT A_sal = ADAMS sal (or) ADAMS A_sal + SCOTT sal


What do you mean by "or" here?

Please show your desired output.

[Updated on: Tue, 21 April 2015 05:58]

Report message to a moderator

Re: Hierarchical queries with aggregation [message #636313 is a reply to message #636311] Tue, 21 April 2015 06:04 Go to previous messageGo to next message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If you don't want to use MODEL clause or... you have to use a hierarchical query in the SELECT part of the hierarchical query that gives you the first output of your post.
Something like:
col ename format a15
SQL> select rpad('-',2*(level-1),'-')||ename ename, sal,
  2         ( select sum(sal) 
  3           from emp e2 
  4           connect by prior empno = mgr 
  5           start with e2.empno = emp.empno ) a_sal
  6  from emp
  7  connect by prior empno = mgr
  8  start with mgr is null
  9  /
ENAME                  SAL      A_SAL
--------------- ---------- ----------
KING                  5000      29025
--JONES               2975      10875
----SCOTT             3000       4100
------ADAMS           1100       1100
----FORD              3000       3800
------SMITH            800        800
--BLAKE               2850       9400
----ALLEN             1600       1600
----WARD              1250       1250
----MARTIN            1250       1250
----TURNER            1500       1500
----JAMES              950        950
--CLARK               2450       3750
----MILLER            1300       1300

Re: Hierarchical queries with aggregation [message #636318 is a reply to message #636313] Tue, 21 April 2015 08:02 Go to previous message
grpatwari
Messages: 288
Registered: June 2008
Location: Hyderabad
Senior Member
Thank you Michel Cadot. I will try with your query.

I am expecting the same output which Michel Cadot has given.
Previous Topic: Convert date to weeks in the month and days
Next Topic: UTL File reader
Goto Forum:
  


Current Time: Wed Apr 24 05:57:59 CDT 2024