基于相似数据点的公共行的变化数
Number of changes in common rows based on similar data point
我有两个问题。第一个检查是否有任何变化的列(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;
我已经能够操纵输出显示 'defect rates' 每个设施和入院日期(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
如有任何帮助,我们将不胜感激 - 谢谢。
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 语句中。
希望能帮助到你。
此外,您的代码示例是重复的,仅供参考
我有两个问题。第一个检查是否有任何变化的列(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;
我已经能够操纵输出显示 'defect rates' 每个设施和入院日期(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
如有任何帮助,我们将不胜感激 - 谢谢。
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 语句中。 希望能帮助到你。 此外,您的代码示例是重复的,仅供参考