如何比较SQL Server中的两个表将值添加到匹配的Employee中

时间:2017-04-26 09:02:59

标签: sql-server

我在SQL Server中有两个表,Table1Table2。两个表都有一列EmployeeName,几乎可以匹配两个表。

这是表1中的样本数据:

EmployeeName   Expenses   Bonus  overTime    
-----------------------------------------
John             198      3734     230
Ayesa              0      2384       0
Nimi             400       230       0
Michael            0       571       0
Alexander        212       240       0

表2 Employee Name几乎具有相同的值 - 如下所示:

EmployeeName     Ermatch      Roth       
-----------------------------------
John               298         500
Ayesa                0         450
Nimi               400         250      
Michael              0         551      

现在,作为员工的两个表都匹配Table 1Table 2。最后像这样加入一个表

 EmployeeName   Expenses   Bonus  overTime    Ermatch  Roth
 ----------------------------------------------------------
 John             198       3734    230         298    500
 Ayesa              0       2384      0           0    450
 Nimi             958        230      0         400    250
 Michael            0        571      0           0    551
 Alexander        212        240      0           0      0

哪个是Employeename匹配表Ermatch和Roth连接表1.如何加入两个表?如何比较表Employeename。提前谢谢

3 个答案:

答案 0 :(得分:2)

你也可以将这两个表加入如下所示的完全连接。当一个表有值但另一个表没有这个值时使用。

;WITH Tab1( EmployeeName,Expenses,Bonus,overTime)
     AS (                         
     SELECT 'John'     ,    198   , 3734  ,   230 union all
     SELECT 'Ayesa'    ,      0   ,  2384 ,    0  union all
     SELECT 'NImi'     ,     400  ,  230  ,    0  union all
     SELECT 'Michael'  ,       0  ,   571 ,     0 union all
     SELECT 'Alexander',     212  ,   240 ,     0 
     )
     ,tab2( EmployeeName,Ermatch,Roth)
     AS
     (                      

     SELECT 'John'     ,298     ,500  Union all
     SELECT 'Ayesa'    , 0      ,450  Union all
     SELECT 'NImi'     , 400    ,250  Union all
     SELECT 'Michael'  , 0      ,551  union all
     SELECT 'Alex'  , 300      ,500
     )
     SELECT isnull(T1.EmployeeName,T2.EmployeeName) EmployeeName,
            ISNULL(T1.Expenses,0)Expenses,
            ISNULL(T1.Bonus,0)Bonus,
            ISNULL(T1.overTime,0)overTime,
            ISNULL(T2.Ermatch,0)Ermatch,
            ISNULL(T2.Roth,0)Roth 
        FROM Tab1 T1 full join tab2 t2
    ON t2.EmployeeName=T1.EmployeeName

答案 1 :(得分:1)

如下所示加入两个表。此代码可能对您的要求很有用

;WITH Tab1( EmployeeName,Expenses,Bonus,overTime)
     AS (                         
     SELECT 'John'     ,    198   , 3734  ,   230 union all
     SELECT 'Ayesa'    ,      0   ,  2384 ,    0  union all
     SELECT 'NImi'     ,     400  ,  230  ,    0  union all
     SELECT 'Michael'  ,       0  ,   571 ,     0 union all
     SELECT 'Alexander',     212  ,   240 ,     0 
     )
     ,tab2( EmployeeName,Ermatch,Roth)
     AS
     (                      

     SELECT 'John'     ,298     ,500  Union all
     SELECT 'Ayesa'    , 0      ,450  Union all
     SELECT 'NImi'     , 400    ,250  Union all
     SELECT 'Michael'  , 0      ,551
     )
     SELECT T1.EmployeeName,
            ISNULL(T1.Expenses,0)Expenses,
            ISNULL(T1.Bonus,0)Bonus,
            ISNULL(T1.overTime,0)overTime,
            ISNULL(T2.Ermatch,0)Ermatch,
            ISNULL(T2.Roth,0)Roth 
        FROM Tab1 T1 LEFT join tab2 t2
    ON t2.EmployeeName=T1.EmployeeName

答案 2 :(得分:1)

如果你想要两个表中的数据,我认为你需要的是FULL JOIN

DECLARE @Table1 as Table (EmployeeName varchar(20),   Expenses int,  Bonus  int ,overTime     int)

INSERT INTO @Table1
VALUES
  ('John',             198,     3734,    230),
  ('Ayesa',              0,     2384,      0),
  ('Nimi',             400,      230,      0),
  ('Michael',            0,      571,      0),
  ('Alexander',        212,      240,      0)

DECLARE @Table2 AS TABLE (EmployeeName varchar(20),   Ermatch   int,  Roth   int )
INSERT INTO @Table2
VALUES 
  ('John',               298,        500),
  ('Ayesa',                0,        450),
  ('Nimi',               400,        250),     
  ('Michael',              0,        551),
  ('NewUser',              0,        551)

SELECT  COALESCE (t.EmployeeName, t2.EmployeeName) AS  EmployeeName,
      ISNULL(t.Expenses,0) AS Expenses, 
      ISNULL(t.Bonus,0) AS Bonus, 
      ISNULL(t.overTime,0) AS overTime, 
      ISNULL(t2.Ermatch,0) AS Ermatch, 
      ISNULL(t2.Roth,0) AS Roth
FROM @Table1 t
FULL JOIN @Table2 t2 ON t.EmployeeName = t2.EmployeeName

演示链接:http://rextester.com/VKJT88878