We have data from scott.emp table:
select empno, ename,mgr, sal from emp order by empno ;
EMPNO|ENAME | MGR| SAL
-----|----------|-----|----------
7369|SMITH | 7902| 800
7499|ALLEN | 7698| 1600
7521|WARD | 7698| 1250
7566|JONES | 7839| 2975
7654|MARTIN | 7698| 1250
7698|BLAKE | 7839| 2850
7782|CLARK | 7839| 2450
7788|SCOTT | 7566| 3000
7839|KING | | 5000
7844|TURNER | 7698| 1500
7876|ADAMS | 7788| 1100
7900|JAMES | 7698| 950
7902|FORD | 7566| 3000
7934|MILLER | 7782| 1300
14 rows selected.
Then I want to list hirearchy of emp as well as Salary Accumulation, which returns rows like this:
DESKRIPSI | EMPNO| MGR| AMOUNT
----------|------|------|--------
KING | 7839| | 29.025
.JONES | 7566| 7839| 10.875
..SCOTT | 7788| 7566| 4.100
...ADAMS | 7876| 7788| 1.100
..FORD | 7902| 7566| 3.800
...SMITH | 7369| 7902| 800
.BLAKE | 7698| 7839| 9.400
..ALLEN | 7499| 7698| 1.600
..WARD | 7521| 7698| 1.250
..MARTIN | 7654| 7698| 1.250
..TURNER | 7844| 7698| 1.500
..JAMES | 7900| 7698| 950
.CLARK | 7782| 7839| 3.750
..MILLER | 7934| 7782| 1.300
14 rows selected.
With Oracle RDBMS, the approach is like this:
WITH pohon
AS (SELECT
DISTINCT CONNECT_BY_ROOT empno parent_id, empno AS id
FROM
emp
CONNECT BY
PRIOR empno = mgr),
trx
AS (SELECT
pohon.parent_id, SUM (tx.sal) AS amount
FROM
pohon JOIN emp tx ON pohon.id = tx.empno
GROUP BY
pohon.parent_id)
SELECT
LPAD (r0.ename, LENGTH (r0.ename) + LEVEL * 1 - 1, '.') AS deskripsi,empno, mgr,
trx.amount
FROM
emp r0 JOIN trx ON r0.empno = trx.parent_id
START WITH
r0.mgr IS NULL
CONNECT BY
r0.mgr = PRIOR r0.empno
;
How can I get the same result in DB2 RDBMS?
Regards.