0

以下のクエリが必要です。

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

しかし、私はエラーが発生します:

集計またはサブクエリを含む式に対して集計関数を実行することはできません。

4

2 に答える 2

1

使用UNION(重複を削除) またはUNION ALL:

SELECT x.groupId, x.Count, x.Type FROM
(    
    Select GroupId,
           Count = count(distinct GroupProgramYearParticipantID),
           Type  = '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

    UNION ALL

    Select GroupId,
           Count = count(distinct GroupProgramYearParticipantID),
           Type  = '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

    UNION ALL

    Select GroupId,
           Count = count(distinct GroupProgramYearParticipantID),
           Type  = '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

    UNION ALL

    Select GroupId,
           Count = count(distinct GroupProgramYearParticipantID),
           Type  = '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

) X

列を追加し、一意の列名を持つ列Typeを に変更したことに注意してください。行のソースを特定するために、列を追加しました。必要に応じて、これらの列のいずれかで並べ替え/フィルター処理することもできます。countCountType

于 2013-10-11T16:20:38.443 に答える