我有两个问题。第一个检查任何更改的列(FirstName,LastName,Phone)基于另一列是相同的(EncounterID):
select FacilityCode, AdmitDate,
sum(case when NumFirstName <> 1 then 0 else 1 end) as DifferentFirstNames,
sum(case when NumLastName <> 1 then 0 else 1 end) as DifferentLastNames,
sum(case when NumPhone <> 1 then 0 else 1 end) as DifferentPhone ,
'Non-Defect' Label
from (select EncounterId, FacilityCode, AdmitDate, count(*) as Num,
count(distinct left(FirstName,4)) as NumFirstName,
count(distinct LastName) as NumLastName,
count(distinct Phone) as NumPhone,
from [BINextGen].[dbo].[tbFCCDefectReport]
group by EncounterId, facilitycode, AdmitDate
) e
group by FacilityCode, AdmitDate;
第二个检查列不变的所有实例:
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 NumPhone <> 1 then 1 else 0 end) as DifferentPhone ,
'Non-Defect' Label
from (select EncounterId, FacilityCode, AdmitDate, count(*) as Num,
count(distinct left(FirstName,4)) as NumFirstName,
count(distinct LastName) as NumLastName,
count(distinct Phone) as NumPhone,
from [BINextGen].[dbo].[tbFCCDefectReport]
group by EncounterId, facilitycode, AdmitDate
) e
group by FacilityCode, AdmitDate;
我能够操纵输出以显示每个设施的'缺陷率'并允许日期(EncounterID是患者文件的标识符,我试图查看文件中的任何数据是否随时间而变化)。现在我想看看有多少EncounterIDs有任何列更改。见下面的例子:
FacilityCode AdmitDate EncounterID FirstName LastName Phone
SMAL 5/15/15 A01342 Justin Kelley 5551212
SMAL 5/15/15 A01342 Justin Kelly 5551212
SMAL 5/15/15 B53421 John Doe 7771234
SMAL 5/15/15 B53421 John Doe 7771234
当前输出:
FacilityCode DifferentFirstNames DifferentLastNames DifferentPhone Label
SMAL 2 1 2 Non-Defect
SMAL 0 1 0 Defect
期望的输出:
FacilityCode Count Label
SMAL 1 Defect
SMAL 1 Non-Defect
任何帮助都将不胜感激 - 谢谢。
答案 0 :(得分:0)
case
WHen sum(case when NumFirstName <> 1 then 0 else 1 end) >= 1 Then 1
WHEN sum(case when NumLastName <> 1 then 0 else 1 end) >= 1 Then 1
WHEN sum(case when NumPhone <> 1 then 0 else 1 end) >= 1 Then 1
else 0 end as 'Count'
您只需将参数捆绑到case语句中即可。 希望能帮助到你。 您的代码示例也是重复的,只是一个fyi