2013-10-11 15 views
-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在表中,但我希望它在在外部選擇中引用的每個對應的組檢查查詢。內子查詢需要在同一組,但不是在所有組

+1

你的問題到底是什麼? – dcaswell

+0

如果你不能正確地解釋你的問題,我們不會費心去嘗試和幫助。 –

+0

對不起。內部選擇查詢檢查所有組中的記錄,但需要檢入相應的組。 – Aditya

回答

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 
相關問題