2015-06-22 130 views
1

我試圖從一個表中檢索數據,然後將其插入另一個表中。 這是包含以下數據的第一個表格的示例。 tb1是由數據組成的表格。經理和TeamLeader兩個欄目基本上是指例如:Josh由Vik管理,等等。員工也可以成爲另一位僱主的經理。例如,Josh是Nirvan和Deva的經理。從一個表中檢索數據並插入到另一個表中

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

我要的是插入另一個表中的所有這些記錄有以下的列:ID(被設置爲IDENTITY/AUTONUM),姓名(員工/經理的姓名),的ParentId(經理的這一個特定的員工必須報告)。 因此,例如, 我應該得到的那種東西:

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 
我有困難可以得到正確的SQL從第一個表中檢索該數據並將其插入到另一個表

+0

你有包含的parentid和經理的名字表? – phicon

+0

不,我只有第一個有數據的表。從那張桌子我必須填寫第二張桌。第二個表中的parent_id基本上是第二個表中的ID本身。例如,由於維克沒有經理必須報告給他,所以他的保證金爲零。 josh,cindy,alvin和kim向Vik報告,因此他們的parentid是vik的ID,它是1 –

回答

1
INSERT INTO tbl2 (
    Name 
    ,parentId 
    ) 
SELECT DISTINCT manager 
    ,0 
FROM tbl1 
WHERE manager NOT IN (
     SELECT employee 
     FROM tbl1 
     ) 

INSERT INTO tbl2 
SELECT DISTINCT employee 
    ,0 
FROM tbl1 

UPDATE tbl2 
SET parentid = parent.id 
FROM tbl2 
INNER JOIN tbl1 ON tbl2.Name = tbl1.employee 
INNER JOIN tbl2 parent ON parent.Name= tbl1.manager 
+0

名稱插入有點令人困惑,但是否則需要解釋爲什麼需要這種方法。例如:這需要分兩步進行;您需要首先插入名稱以獲取唯一的ID –

+0

感謝隊友:)完美地工作 –

2

你必須分兩步做。一個來創建你所有的ID字段。然後,你可以用這個表連接到匹配的員工找到經理,經理的ID:

insert into MyNewTable (Name, ParentID) 
select Manager, ParentID 
from tbl1 
union 
select Employee, ParentID 
from tbl1 

update MyNewTable 
set MyNewTable.ParentId = Managers.Id 
from MyNewTable 
join tbl1 
on tbl1.Employee = MyNewTable.Name 
join MyNewTable Managers 
on MyNewTable.Name = Managers.Manager 
0
create table #tbl1 (manager char(15), employee char(15)) 

create table #tbl2 (ID int identity(1,1), Name char(15), ParentID int) 

insert into #tbl1 
values ('Vik', 'Josh') , 
('Vik'  , 'Cindy') , 
('Vik'  , 'Alvin') , 
('Vik'  , 'Kim') , 
('Josh' , 'Nirvan'), 
('Josh' , 'Deva') , 
('Cindy' ,'Mervyn') , 
('Nirvan' , 'Reeta'), 
('Nirvan' , 'Zaki'), 
('Nirvan' , 'Sunny') 

--- big Boss 
insert into #tbl2(Name, ParentID) 
SELECT DISTINCT manager, 0 
FROM #tbl1 
WHERE manager NOT IN (SELECT employee FROM #tbl1 ) 

--- Grab all Employees (Name) 
insert into #tbl2(Name) 
select distinct employee from #tbl1 

--- Update ParentID 
declare @StructureLevl int = 3 -- 0 1 2 3 Do something to figure out this number first 
declare @Var_int int = 0 
while (@Var_int < @StructureLevl) 
begin 
    update #tbl2 
    set ParentID= @Var_int +1 
    where Name in (select A.employee 
        from #tbl1 A inner join #tbl2 B on A.manager = B.Name 
        where B.ParentID = @Var_int) 

    set @Var_int +=1 
end 

select * from #tbl2 
order by ParentID 
相關問題