-3
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

内部选择查询检查表中的所有 groupid,但我希望它检查外部选择查询中引用的每个相应组。

4

1 回答 1

0

您需要在子查询中添加 where 子句以通过其 groupId 引用主体查询,以便获得您正在寻找的结果。

Select GroupId,count(distinct GroupProgramYearParticipantID)as [ChildAddedcurrent]  
from #temp1 
Where (MonthFlag = 0) and (ParticipantTypeName = 'child')  
and (GroupProgramYearParticipantID not in 
(Select distinct GroupProgramYearParticipantID 
from #temp1 t
Where (t.MonthFlag = 1) and (t.ParticipantTypeName = 'child') and t.GroupId = GroupId)) 
group by groupId
于 2013-10-11T19:33:08.910 回答