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列并将count具有唯一列名的列更改为Count. 为了确定行的来源,我添加了Type列。现在,如果需要,您甚至可以按这些列之一进行排序/过滤。

于 2013-10-11T16:20:38.443 回答
0

Aaron 关于解析错误是正确的。

此外,NOT IN 子句的问题意味着您必须检查曾经的 id 以排除一行。当数据很大时,这是一项非常昂贵的操作。

使用带有 SET 运算符的公用表表达式 CTE 是您的关键。请参阅我关于集合运算符的文章。

http://craftydba.com/?p=5617

首先,为每个类别(儿童、看护人)创建每个月(0、1、2)的 CTE。

;
-- Child +0M
with cteChildMonth0
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=0 and ParticipantTypeName='child'
),

-- Child +1M
cteChildMonth1
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=1 and ParticipantTypeName='child'
),

-- Child +2M
cteChildMonth2
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=2 and ParticipantTypeName='child'
),

-- Giver +0M
cteCareGiverMonth0
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=0 and ParticipantTypeName='caregiver'
),

-- Giver +1M
cteCareGiverMonth1
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=1 and ParticipantTypeName='caregiver'
),

-- Giver +2M
cteCareGiverMonth2
as
(
select groupID as group_id, GroupProgramYearParticipantID as participant_id
from #temp1
where MonthFlag=2 and ParticipantTypeName='caregiver'
),

其次,使用前 6 个 CTE 和 SET 符号运算符 EXCEPT 创建当前与先前的 CTE。这是在表级别完成的,它应该比 NOT IN 更快。对于每一行,为报表创建一个虚拟计数器 = 1。

-- Child add current
cteChildAddCurrent
(
select group_id, 'Child Current' as category_txt, 1 as participant_cnt
from cteChildMonth0 except cteChildMonth0 
),

-- Child add prior
cteChildAddPrior
(
select group_id, 'Child Prior' as category_txt, 1 as participant_cnt
from cteChildMonth1 except cteChildMonth2
),

-- Giver add current
cteGiverAddCurrent
(
select group_id, 'Giver Current' as category_txt, 1 as participant_cnt
from cteCareGiverMonth0 except cteCareGiverMonth1
),

-- Giver add prior
cteGiverAddPrior
(
select group_id, 'Giver Prior' as category_txt, 1 as participant_cnt
from cteCareGiverMonth1 except cteCareGiverMonth2
),

第三,将 4 个类别合并为一张表。

-- Giver add prior
cteSummaryRpt
(
select * from cteChildAddCurrent
union all
select * from cteChildAddPrior
union all
select * from cteGiverAddPrior
union all
select * from cteGiverAddCurrent
)

最后但同样重要的是,按 id、类别文本和参与者计数分组。

select group_id, category_txt, sum(participant_cnt) as total
from cteSummaryRpt
group by group_id, category_txt

我将这个解决方案分成几个部分来解释它,但实际上这是一个大问题。此外,由于您没有提供示例表和/或数据,请检查语法,因为我没有。

祝你好运。

于 2013-10-11T20:07:15.260 回答