I am trying to map to retrieve data from one table and then insert it into another table. This is a a sample of the first table in which there is the following data. tb1 is the table which consists of data. The two columns Manager and TeamLeader basically means for example : Josh is managed by Vik and so on. An employee can also be a manager to another employer. For example, Josh is the manager of Nirvan and Deva.

+---------+-------------+
|        tbl1           |
+---------+-------------+
| Manager | Employee    |
+---------+-------------+
| Vik     | Josh        |
+---------+-------------+
| Vik     | Cindy       |
+---------+-------------+
| Vik     | Alvin       |
+---------+-------------+
| Vik     | Kim         |
+---------+-------------+
| Josh    | Nirvan      |
+---------+-------------+
| Josh    | Deva        |
+---------+-------------+
| Cindy   | Mervyn      |
+---------+-------------+
| Nirvan  | Reeta       |
+---------+-------------+
| Nirvan  | Zaki        |
+---------+-------------+
| Nirvan  | Sunny       |
+---------+-------------+

What i want is to insert all these records in another table with the following columns : Id(which is set to IDENTITY/AUTONUM), Name(name of employee/manager), ParentId(of the manager which a particular employee has to report to). So for example, I should be getting something of the sort :

ID Name ParentId
1  Vik    0
2  Josh   1
3  Cindy  1
4  Alvin  1
5  Kim    1
6  Nirvan 2
7  Deva   2
8  Mervyn 3
9  Reeta  6
10 Zaki   6
11 Sunny  6

I am having difficulty to get the right sql to retrieve this data from the first table and insert it into another table.

Related posts

Recent Viewed