Select GroupId,
count(distinct GroupProgramYearParticipantID)as [ChildAddedprior]
from #temp1
Where (MonthFlag = '1')
and (ParticipantTypeName = 'child')
and (GroupProgramYearParticipantID not in
(Select distinct GroupProgramYearParticipantID
from #temp1 t
Where (t.MonthFlag = '2')
and (t.ParticipantTypeName = 'child')
and t.GroupId = GroupId))
group by groupId
groupID 1 が GroupProgramYearParticipantID の 1,2,2,3,4,4 を持ち、groupID 2 が GroupProgramYearParticipantID の 2,4,4,5,5,6,7 を持っている場合
上記のクエリは戻ります
GroupID-1 ChildAddedprior- 4( which takes 1,2,3,4)
DroupID-2 ChildAddedPrior- 5(which includes 2,4,5,6,7)
しかし、私が欲しいのは
GroupID-1 ChildAddedprior- 4 (which takes 1,2,3,4)
DroupID-2 ChildAddedPrior- 3 (which includes 5,6,7)(this doesn't include 2,4 which are counted earlier).
助けていただければ幸いです