Sqlserver
 SQL >> Database >  >> RDS >> Sqlserver

Controllare le modifiche in tutte le altre colonne in base alla somiglianza di una colonna

I dati che vuoi sostanzialmente sono il numero di entità che hanno più di un valore in una colonna.

Questo si calcola più facilmente su base colonna per colonna:

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; 

Puoi formattare i risultati come preferisci.