2015-07-10 49 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等增加的數據(表中不同的數據)。本質上而不是分組查詢,拉* –

相關問題