2015-07-10 44 views
0
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; 

我需要上面的查询来对表中另一列进行分组,名为FacilityCode,以及显示重复EncounterIDs的次数和列没有缺陷。根据列的相似性检查所有其他列中的更改(续)

另外,除了计数(即第一个查询的结果背后的数据)之外,是否可以使用类似构建的查询来拉取“缺陷”结果?

见链接前面的问题: Check for changes in all other columns based on similarities one column

回答

0

是。只要把该列中的子查询和聚集在外部查询:

select FacilityCode, 
     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, FacilityCode, 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, FacilityCode 
    ) e 
group by FacilityCode; 

我不知道你所说的“拉缺陷导致”的意思。但子查询(可能带有合适的having子句)将获得每个EncounterId的信息。

+0

这是我需要的第一部分 - 谢谢你。 –

+0

我所说的“拉出缺陷”的意思是拉取导致DifferentFirstNames,DifferentLastNames等增加的数据(表中不同的数据)。本质上而不是分组查询,拉* –