SQL Union / Join Request

I have a user profile table with columns User Name, Managerand many other fields, for example Amount.

Examples of entries:

User    Manager  Amount
A       B       100
x       y       200
B       C       300
M       N       800
C       D       500
P       Q       1000
D       E       1000

I am trying to get the result as below:

User    Manager Amount
A       B       100
B       C       300
C       D       500
D       E       1000

Basically, I just want to show the results in a cascading way so that all rows are retrieved until they find all the managers in the hierarchy for the user. Can anyone help?

=============

Update

I was able to solve the problem using the following query:

WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 = 'A'
UNION ALL
SELECT table.c1, table.c2 FROM table, rec WHERE table.c2 = rec.c2)
SELECT c1, c2 FROM rec

Thanks for the help. Abi

+3
source share
1 answer

"" sql. SQL, .

Oracle - :

SELECT USER, Manager, Amount FROM employees CONNECT BY PRIOR USER = Manager;

. : http://www.ibm.com/developerworks/data/library/techarticle/dm-0510rielau/ db2

+1

Source: https://habr.com/ru/post/1759098/


All Articles