I have a table employees
id | name | manager |
---|---|---|
1 | susan | null |
2 | john | 1 |
3 | mary | 1 |
4 | josh | 3 |
5 | mathew | 4 |
6 | leo | 2 |
7 | peter | 5 |
I want an output like:
id | name | hierarchy |
---|---|---|
1 | susan | null |
2 | john | susan->john |
3 | mary | susan->mary |
4 | josh | susan->mary->josh |
5 | mathew | susan->mary->josh->mathew |
6 | leo | susan->john->leo |
7 | peter | susan->mary->josh->mathew->peter |
DDL:
create table employees(id int,name varchar(10), manager int);
insert into employees
values(1,'susan',null)
,(2,'john',1)
,(3,'mary',1)
,(4,'josh',3)
,(5,'mathew',4)
,(6,'leo',2)
,(7,'peter',5)