根据一列的相似性检查所有其他列的更改

时间:2015-07-10 15:22:41

标签: sql sql-server-2008

FirstName     LastName     SSN            Phone          EncounterID
Justin        Kelley      555-55-5555     517-555-1212    123456789
Justin        Kelly       555-55-5555     517-555-1212    123456789
James         Smith       444-44-4444     312-555-3434    99944444
James         Smith       444-44-4444     312-555-3434    99944444

我有一张像上面那样有数百万个EncounterID的表。我需要知道EACH列中存在差异(缺陷)的次数。我的示例输出是:

First Name - 2/2
Last Name - 1/2
SSN - 2/2
Phone - 2/2

这里有任何帮助吗?

1 个答案:

答案 0 :(得分:4)

您基本需要的数据是列中具有多个值的实体数。

这最容易按列计算:

select sum(case when NumFirstNames <> 1 then 1 else 0 end) as DifferentFirstNames,
       sum(case when NumLastNames <> 1 then 1 else 0 end) as DifferentLastNames,
       sum(case when NumSSN <> 1 then 1 else 0 end) as DifferentSSN,
       sum(case when NumPhone <> 1 then 1 else 0 end) as DifferentPhone       
from (select EncounterId, count(*) as Num,
             count(distinct FirstName) as NumFirstNames,
             count(distinct LastName) as NumLastNames,
             count(distinct SSN) as NumSSN,
             count(distinct Phone) as NumPhone
      from table t
      group by EncounterId
     ) e;

您可以根据需要格式化结果。