我想要以下查询:
Select groupId,count (distinct GroupProgramYearParticipantID) as [ChildAddedcurrent]
from #temp1 Where MonthFlag=0 and ParticipantTypeName='child'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=1 and ParticipantTypeName='child')
group by groupId
Select groupId,count (distinct GroupProgramYearParticipantID) as [CaregiverAddedcurrent]
from #temp1 Where MonthFlag=0 and ParticipantTypeName='caregiver'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=1 and ParticipantTypeName='caregiver')
group by groupId
Select groupId,count (distinct GroupProgramYearParticipantID) as [ChildAddedprior]
from #temp1 Where MonthFlag=1 and ParticipantTypeName='child'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=2 and ParticipantTypeName='child')
group by groupId
Select groupId,count (distinct GroupProgramYearParticipantID) as [caregiverAddedPrior]
from #temp1 Where MonthFlag=1 and ParticipantTypeName='caregiver'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=2 and ParticipantTypeName='caregiver')
group by groupId
更像这样:
select groupID,
count(distinct case when MonthFlag=0 and ParticipantTypeName='child'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=1 and ParticipantTypeName='child') then GroupProgramYearParticipantID end) as [ChildAddedcurrent],
count(distinct case when MonthFlag=0 and ParticipantTypeName='caregiver'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=1 and ParticipantTypeName='caregiver') then GroupProgramYearParticipantID end) as [CaregiverAddedcurrent],
count(distinct case when MonthFlag=1 and ParticipantTypeName='child'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=2 and ParticipantTypeName='child')then GroupProgramYearParticipantID end) as [ChildAddedprior],
count(distinct case when MonthFlag=1 and ParticipantTypeName='caregiver'
and GroupProgramYearParticipantID not in (Select distinct GroupProgramYearParticipantID from #temp1
Where MonthFlag=2 and ParticipantTypeName='caregiver') then GroupProgramYearParticipantID end) as [caregiverAddedPrior]
From #temp1
group by groupID
但我收到一个错误:
无法对包含聚合或子查询的表达式执行聚合函数。