-1

I have the following tables

Lead

  • id varchar
  • employee_id varchar

Employee

  • id varchar
  • lead_id varchar

There will be a group of employees assigned to a lead. The Lead table holds the employee id of the lead. The employee table will have lead_id which will be the id key of the leader.

The table will also contain employees which are not assigned to any lead

I need a query which will display the hierarchical result which will list the leaders and the employees under the leader

leader1 (employee )

  • employee1
  • employee 2

Leader 2(employee)

  • employee 3
  • employee 4

Any idea how this kind of hierarchical result can be obtained by a db2 query?

Click on the this link to view the table structure

Sachi
  • 1
  • 1
  • Hierarchical queries are pretty simple in DB2. Do you want just two levels (as in your example), or all levels? If you add some sample data I can write the response in no time. – The Impaler Jun 28 '18 at 17:44
  • yes, I just need 2 levels , I am editing the question to add sample data you requested – Sachi Jun 28 '18 at 23:50

1 Answers1

1

The answer is a join of the two tables like

SELECT l.employee_id as leader_employee_id, e.id as employee_id  
  FROM LEAD l
 INNER JOIN EMPLOYEE e
    ON e.lead_id = l.employee_id 
MichaelTiefenbacher
  • 3,805
  • 2
  • 11
  • 17