使用子查询拉取查询后面的所有数据

时间:2015-07-11 16:34:21

标签: sql-server sql-server-2008-r2

我有下面的查询,这非常适合告诉我在其他列上链接的数据列的差异。现在我需要能够提取所有数据,其中sum(case)增加计数器以验证结果(基本上删除分组但仅包括我正在寻找的数据)。

此外,任何简化原始查询的帮助都会很棒,因为它不必要很长。

select FacilityCode, AdmitDate,
sum(case when NumFirstName <> 1 then 1 else 0 end) as DifferentFirstNames,
sum(case when NumLastName <> 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 ,  
sum(case when NumSex <> 1 then 1 else 0 end) as DifferentSex   ,
sum(case when NumPatientDOB <> 1 then 1 else 0 end) as DifferentPatientDOB   ,
sum(case when NumAddress <> 1 then 1 else 0 end) as DifferentAddress   ,
sum(case when NumAddress2 <> 1 then 1 else 0 end) as DifferentAddress2   ,
sum(case when NumCity <> 1 then 1 else 0 end) as DifferentCity   ,
sum(case when NumState <> 1 then 1 else 0 end) as DifferentState   ,
sum(case when NumZip <> 1 then 1 else 0 end) as DifferentZip   ,
sum(case when NumMaritalStatus <> 1 then 1 else 0 end) as DifferentMaritalStatus   , 
sum(case when NumReligion <> 1 then 1 else 0 end) as DifferentReligion   ,
sum(case when NumRace <> 1 then 1 else 0 end) as DifferentRace   ,
sum(case when NumEmail <> 1 then 1 else 0 end) as DifferentEmail   ,
sum(case when NumID <> 1 then 1 else 0 end) as DifferentID   ,
sum(case when NumPatientType <> 1 then 1 else 0 end) as DifferentPatientType   ,
sum(case when NumFacilityPatientID <> 1 then 1 else 0 end) as DifferentFacilityPatientID   ,
sum(case when NumServiceFieldCode <> 1 then 1 else 0 end) as DifferentServiceFieldCode   ,
sum(case when NumReferringPhysicianName <> 1 then 1 else 0 end) as DifferentReferringPhysicianName   ,
sum(case when NumReferringPhysicianPhone <> 1 then 1 else 0 end) as DifferentReferringPhysicianPhone   ,
sum(case when NumAdmittingPhysicianName <> 1 then 1 else 0 end) as DifferentAdmittingPhysicianName   ,
sum(case when NumAdmittingPhysicianPhone <> 1 then 1 else 0 end) as DifferentAdmittingPhysicianPhone   ,
sum(case when NumGuarantorFirstName <> 1 then 1 else 0 end) as DifferentGuarantorFirstName   ,
sum(case when NumGuarantorLastName <> 1 then 1 else 0 end) as DifferentGuarantorLastName   ,
sum(case when NumGuarantorPhone <> 1 then 1 else 0 end) as DifferentGuarantorPhone   ,
sum(case when NumGuarantorAddress <> 1 then 1 else 0 end) as DifferentGuarantorAddress   ,
sum(case when NumGuarantorAddress2 <> 1 then 1 else 0 end) as DifferentGuarantorAddress2   ,
sum(case when NumGuarantorZip <> 1 then 1 else 0 end) as DifferentGuarantorZip   ,
sum(case when NumGuarantorState <> 1 then 1 else 0 end) as DifferentGuarantorState   ,
sum(case when NumGuarantorCity <> 1 then 1 else 0 end) as DifferentGuarantorCity   ,
sum(case when NumEmployerName <> 1 then 1 else 0 end) as DifferentEmployerName   ,
sum(case when NumEmployerPhone <> 1 then 1 else 0 end) as DifferentEmployerPhone   ,
sum(case when NumERContactFirstName <> 1 then 1 else 0 end) as DifferentERContactFirstName   ,
sum(case when NumERContactLastName <> 1 then 1 else 0 end) as DifferentERContactLastName   ,
sum(case when NumERPhone <> 1 then 1 else 0 end) as DifferentERPhone   ,
sum(case when NumGuarantorDOB <> 1 then 1 else 0 end) as DifferentGuarantorDOB   ,
sum(case when NumGuarantorEmployer <> 1 then 1 else 0 end) as DifferentGuarantorEmployer   ,
sum(case when NumGuarantorEmployerPhone <> 1 then 1 else 0 end) as DifferentGuarantorEmployerPhone   ,
sum(case when NumGuarantorRace <> 1 then 1 else 0 end) as DifferentGuarantorRace   ,
sum(case when NumHasBenefitsForResidual <> 1 then 1 else 0 end) as DifferentHasBenefitsForResidual   ,
sum(case when NumPrimaryType <> 1 then 1 else 0 end) as DifferentPrimaryType   ,
sum(case when NumPrimaryPayor <> 1 then 1 else 0 end) as DifferentPrimaryPayor   ,
sum(case when NumPrimaryPlan <> 1 then 1 else 0 end) as DifferentPrimaryPlan   ,
sum(case when NumPrimaryAuto <> 1 then 1 else 0 end) as DifferentPrimaryAuto   ,
sum(case when NumPrimaryStatus <> 1 then 1 else 0 end) as DifferentPrimaryStatus   ,
sum(case when NumPrimaryVerificationStatus <> 1 then 1 else 0 end) as DifferentPrimaryVerificationStatus   ,
sum(case when NumHasSecondary <> 1 then 1 else 0 end) as DifferentHasSecondary   ,
sum(case when NumSecondaryType <> 1 then 1 else 0 end) as DifferentSecondaryType   ,
sum(case when NumSecondaryPayor <> 1 then 1 else 0 end) as DifferentSecondaryPayor   ,
sum(case when NumSecondaryPlan <> 1 then 1 else 0 end) as DifferentSecondaryPlan   ,
sum(case when NumSecondaryAuto <> 1 then 1 else 0 end) as DifferentSecondaryAuto   ,
sum(case when NumSecondaryStatus <> 1 then 1 else 0 end) as DifferentSecondaryStatus   ,
sum(case when NumSecondaryVerificationStatus <> 1 then 1 else 0 end) as DifferentSecondaryVerificationStatus   ,
sum(case when NumHasTertiary <> 1 then 1 else 0 end) as DifferentHasTertiary   ,
sum(case when NumTertiaryType <> 1 then 1 else 0 end) as DifferentTertiaryType   ,
sum(case when NumTertiaryPayor <> 1 then 1 else 0 end) as DifferentTertiaryPayor   ,
sum(case when NumTertiaryPlan <> 1 then 1 else 0 end) as DifferentTertiaryPlan   ,
sum(case when NumTertiaryAuto <> 1 then 1 else 0 end) as DifferentTertiaryAuto   ,
sum(case when NumTertiaryStatus <> 1 then 1 else 0 end) as DifferentTertiaryStatus   ,
sum(case when NumTertiaryVerificationStatus <> 1 then 1 else 0 end) as DifferentTertiaryVerificationStatus   

from (select EncounterId, FacilityCode, AdmitDate, count(*) as Num,
count(distinct FirstName) as NumFirstName,
count(distinct LastName) as NumLastName,
count(distinct SSN) as NumSSN,
count(distinct Phone) as NumPhone,
count(distinct Sex) as NumSex,
count(distinct PatientDOB) as NumPatientDOB,
count(distinct Address) as NumAddress,
count(distinct Address2) as NumAddress2,
count(distinct City) as NumCity,
count(distinct State) as NumState,
count(distinct Zip) as NumZip,
count(distinct MaritalStatus) as NumMaritalStatus,
count(distinct Religion) as NumReligion,
count(distinct Race) as NumRace,
count(distinct Email) as NumEmail,
count(distinct ID) as NumID,
count(distinct PatientType) as NumPatientType,
count(distinct FacilityPatientID) as NumFacilityPatientID,
count(distinct ServiceFieldCode) as NumServiceFieldCode,
count(distinct ReferringPhysicianName) as NumReferringPhysicianName,
count(distinct ReferringPhysicianPhone) as NumReferringPhysicianPhone,
count(distinct AdmittingPhysicianName) as NumAdmittingPhysicianName,
count(distinct AdmittingPhysicianPhone) as NumAdmittingPhysicianPhone,
count(distinct GuarantorFirstName) as NumGuarantorFirstName,
count(distinct GuarantorLastName) as NumGuarantorLastName,
count(distinct GuarantorPhone) as NumGuarantorPhone,
count(distinct GuarantorAddress) as NumGuarantorAddress,
count(distinct GuarantorAddress2) as NumGuarantorAddress2,
count(distinct GuarantorZip) as NumGuarantorZip,
count(distinct GuarantorState) as NumGuarantorState,
count(distinct GuarantorCity) as NumGuarantorCity,
count(distinct EmployerName) as NumEmployerName,
count(distinct EmployerPhone) as NumEmployerPhone,
count(distinct ERContactFirstName) as NumERContactFirstName,
count(distinct ERContactLastName) as NumERContactLastName,
count(distinct ERPhone) as NumERPhone,
count(distinct GuarantorDOB) as NumGuarantorDOB,
count(distinct GuarantorEmployer) as NumGuarantorEmployer,
count(distinct GuarantorEmployerPhone) as NumGuarantorEmployerPhone,
count(distinct GuarantorRace) as NumGuarantorRace,
count(distinct HasBenefitsForResidual) as NumHasBenefitsForResidual,
count(distinct PrimaryType) as NumPrimaryType,
count(distinct PrimaryPayor) as NumPrimaryPayor,
count(distinct PrimaryPlan) as NumPrimaryPlan,
count(distinct PrimaryAuto) as NumPrimaryAuto,
count(distinct PrimaryStatus) as NumPrimaryStatus,
count(distinct PrimaryVerificationStatus) as NumPrimaryVerificationStatus,
count(distinct HasSecondary) as NumHasSecondary,
count(distinct SecondaryType) as NumSecondaryType,
count(distinct SecondaryPayor) as NumSecondaryPayor,
count(distinct SecondaryPlan) as NumSecondaryPlan,
count(distinct SecondaryAuto) as NumSecondaryAuto,
count(distinct SecondaryStatus) as NumSecondaryStatus,
count(distinct SecondaryVerificationStatus) as NumSecondaryVerificationStatus,
count(distinct HasTertiary) as NumHasTertiary,
count(distinct TertiaryType) as NumTertiaryType,
count(distinct TertiaryPayor) as NumTertiaryPayor,
count(distinct TertiaryPlan) as NumTertiaryPlan,
count(distinct TertiaryAuto) as NumTertiaryAuto,
count(distinct TertiaryStatus) as NumTertiaryStatus,
count(distinct TertiaryVerificationStatus) as NumTertiaryVerificationStatus

from [BINextGen].[dbo].[tbFCCDefectReport]

group by EncounterId, facilitycode, AdmitDate
 ) e
group by FacilityCode, AdmitDate;

0 个答案:

没有答案