Reputation: 12369
UPDATE: Adding more Employee
and ReplacementEmployee
Table Employee
-
EmployeeId SerialNo
-----------------------------
1 11111
34 23233
23 13234
Table ReplacementEmployee
-
ReplacementId SerialNo
-----------------------------
11 11111
23 23233
13 13234
So when I join these tables on Serial No. have something like this Mapping Table
-
EmployeeId ReplacementId
-----------------------------
1 11
34 23
23 13
So now I have insert all the data from table something like this OrignalTable
and it can have multiple values for EmployeeId-
EmployeeId Name
-----------------------------
1 AAA
34 BBB
23 CCC
1 AAA
Into an table with different schema with basically something like this InsertTable
and it can have multiple values for EmployeeId-
ReplacementId Name
-------------------------------
11 AAA
23 BBB
13 CCC
11 AAA
So if you look at the problem I need to insert
data from one table to other but I need to insert a different field i.e. in this case I need to insert the corresponding
ReplacementId
instead of the EmployeeId
in the new table. So can anyone suggest me how would I go about writing such a query ?
Upvotes: 1
Views: 103
Reputation: 32690
Try this:
INESRT INTO InsertTable (ReplacementID, [Name])
SELECT R.ReplacementID, O.[Name]
FROM OriginalTable O
INNER JOIN Employee E ON E.EmployeeID = O.EmployeeID
INNER JOIN ReplacementEmployee R ON E.SerialNo = R.SerialNo
And if you only want ONE instance of each employee in the InsertTable, use SELECT DISTINCT
instead
Upvotes: 2