2013-10-11 25 views
0

我想下面的查詢:很熱將4個查詢結合到一個沒有具有多個條件的子查詢中?

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 

但我得到一個錯誤:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

+0

只是確認預期的結果。在每個查詢中都有一個Where過濾器「和GroupProgramYearParticipantID不在」中。過濾此GroupProgramYearParticipantID的子查詢不考慮GroupID,但Outer Most查詢使用GroupBY GroupID列。你確定在爲IN子句過濾「GroupProgramYearParticipantID」時,結果應該查看所有的GroupID嗎? –

+0

它應該只在相應的組中查找。 – Aditya

回答

0

亞倫對解析錯誤是正確的。

此外,NOT IN子句的問題意味着您必須檢查ever ID以排除一行。數據量大時,這是非常昂貴的操作。

使用公用表表達式使用SET運算符的CTE是您的關鍵。看到我關於集合運營商的文章。

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

首先,CTE的創建每月(0,1,2)每個類別(兒童,照顧者)。

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

我把這個解決方案分成了幾部分來解釋它,但實際上它是一個大的問題。另外,由於您沒有提供示例表格和/或數據,請檢查語法,因爲我沒有。

祝你好運。

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列。 現在,您甚至可以根據需要訂購/過濾這些列之一。

+0

我希望這些查詢在使用case語句的單個查詢中。你可以請現在檢查我的問題。 – Aditya

+1

你爲什麼要這麼做?它當然不會更具可讀性,並且可能不會表現得更好(除非有原因)。 –