仅显示table1中与table2不同的字段(Sql server 2008)

时间:2014-03-07 20:22:39

标签: sql-server-2008 except

我每天收到一个文件,可能会或者可能没有前一天的更改,我复制我收到的文件并将其称为table2,第二天我收到文件我将它加载到table1但现在我想看到发生了什么变化。问题是,我不想看到整行,我只是想看看发生了什么变化。

CREATE TABLE Table1
([Emp_ID] int, [DOB] date, [code] varchar(10))
;

INSERT INTO Table1
 ([Emp_ID], [DOB], [code])
VALUES
 (55556, '1966-01-15', '5454'),
 (55557, '1980-03-21', '6868'),
 (55558, '1985-04-26', '7979'),
 (55559, '1990-10-17', '1212'),
 (55560, '1992-12-30', '6767')
;
CREATE TABLE Table2
([Emp_ID] int, [DOB] date, [code] varchar(10))
;

INSERT INTO Table2
 ([Emp_ID], [DOB], [code])
VALUES
 (55556, '1966-01-15', '5454'),
 (55557, '1980-03-21', '6868'),
 (55558, '1985-04-26', '7979'),
 (55559, '1990-10-17', '1212'),
 (55560, '1992-12-30', '5555')
;

现在如果我使用EXCEPT功能,我会看到

select * from table1
except
select * from table2

EMP_ID  DOB         CODE
55560   1992-12-30  6767

但是如果我只是想看到EMP_ID和已经改变的字段,那么我想看到这个结果。

EMP_ID  DOB         CODE
55560               6767

因为代码是针对该EMP_ID而改变的所有内容吗?

提前感谢任何建议!

4 个答案:

答案 0 :(得分:1)

您可以使用Common Table Expression (CTE)来确定差异。然后将这些结果与初始表结合使用,在每个字段上使用CASE表达式来确定特定字段是否已更改。

;with UpdatedRecords as
( select * from table1
  except
  select * from table2
)
select ur.EMP_ID, 
       CASE WHEN ur.DOB <> t2.DOB THEN ur.DOB ELSE NULL END as DOB,
       CASE WHEN ur.code <> t2.code THEN ur.code ELSE NULL END as code

from UpdatedRecords ur
join table2 t2 on ur.EMP_ID=t2.EMP_ID

答案 1 :(得分:1)

试试这个:

SELECT * FROM [Table1] tb1
WHERE tb1.[EMP_ID] not in (select [EMP_ID] from [Table1])  

它对我有用。

但是这个例子仅用于ID比较。小心

<强> EDITED

我发现one article&amp;我希望它能帮助您解决问题:

示例:

(select * from A 
 minus
 select * from B) -- Rows in A that are not in B
 union all
  ( 
   select * from B 
   minus
   select * from A
  ) -- rows in B that are not in A

您还可以看到this question

答案 2 :(得分:1)

完全加入可能比在这种情况下更好;这也将处理删除和插入

 select coalesce(t1.emp_id, t2.emp_id),
 case when isnull(t1.dob,'')<>isnull(t2.dob,'')  then t2.dob else null end  DOB,
 case when isnull(t1.code,'')<>isnull(t2.code,'') then t2.code else null end  Code
 from table1 t1
 full join table2 t2 on t1.emp_id=t2.emp_id
 where isnull(t1.dob,'')<>isnull(t2.dob,'') or isnull(t1.code,'')<>isnull(t2.code,'')

答案 3 :(得分:1)

此数据按摩脚本适用于您的情况下的样本数据。你可以调整它。基本思路是找出不同列的内容。

with u as 
(   
    select emp_id, dob, code  from table1
    union
    select emp_id,  dob, code   from table2
),
d as 
(
    select emp_id, dob, code  from table1
    except
    select emp_id,  dob, code   from table2
),
c as
(
    select u.* from u inner join d
        on u.emp_id = d.emp_id and u.dob = d.dob            
    union
    select u.* from u inner join d
        on u.emp_id = d.emp_id and u.code = d.code
),
w as
(
    select c.emp_id, c.dob, c.code, 'CodeDiff' as Diff 
    from c inner join d
        on c.emp_id = d.emp_id and c.code <> d.code
            and c.dob = d.dob
    union 
    select c.emp_id, c.dob, c.code, 'DobDiff' as Diff 
    from c inner join d
        on c.emp_id = d.emp_id and c.code = d.code
            and c.dob <> d.dob
)
select 
    d.emp_id, 
    case w.Diff 
        when 'DobDiff' then cast(d.dob AS varchar(25))
        else ''
    end as dob,
    case w.Diff 
        when 'CodeDiff' then d.code
        else ''
    end as code
from d inner join w
    on d.emp_id = w.emp_id and d.code <> w.code
        and d.dob = w.dob