我有大约2页的查询,这很简单没有复杂的逻辑,但是当我在sql server中运行该查询时,执行需要10-15秒,我已经应用了所需的所有索引查询,仍然需要加载,任何人都可以告诉我,我可以做更多的事情,这有助于我更快地执行查询,这是我的查询
SELECT
COUNT(CASE when (SOC = '1' and MonthStart='2016-05') then 1 ELSE NULL END) as SOCCount_2016_05,
COUNT(CASE when (Recert = '1' and MonthStart='2016-05') then 1 ELSE NULL END) as RecertCount_2016_05,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-05') then 1 ELSE NULL END)) as RecertPer1_2016_05,
COUNT(CASE when (MonthStart='2016-05') then 1 ELSE NULL END) as TotalEpisode_2016_05,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-05') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_05,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-05') then CMW ELSE NULL END),3) as AvgCMW_2016_05,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-05') then 1 ELSE NULL END) as LUPAs_2016_05,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-05') then 1 ELSE NULL END) as LUPAPer_2016_05,
COUNT(CASE when ((MonthEnd='2016-05' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_05,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-05') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_05,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-05') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_05,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-05') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_05,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-05') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_05,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-05') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_05,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-05') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_05,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-05') then 1 ELSE NULL END) as Lupa_Closed_2016_05,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-05') then TH_Ep ELSE NULL END) as TherapyPer_2016_05,
SUM(CASE when (MonthEnd='2016-05') then NonTHEp ELSE NULL END) as NonTHEp_2016_05 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-06') then 1 ELSE NULL END) as SOCCount_2016_06,
COUNT(CASE when (Recert = '1' and MonthStart='2016-06') then 1 ELSE NULL END) as RecertCount_2016_06,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-06') then 1 ELSE NULL END)) as RecertPer1_2016_06,
COUNT(CASE when (MonthStart='2016-06') then 1 ELSE NULL END) as TotalEpisode_2016_06,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-06') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_06,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-06') then CMW ELSE NULL END),3) as AvgCMW_2016_06,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-06') then 1 ELSE NULL END) as LUPAs_2016_06,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-06') then 1 ELSE NULL END) as LUPAPer_2016_06,
COUNT(CASE when ((MonthEnd='2016-06' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_06,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-06') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_06,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-06') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_06,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-06') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_06,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-06') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_06,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-06') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_06,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-06') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_06,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-06') then 1 ELSE NULL END) as Lupa_Closed_2016_06,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-06') then TH_Ep ELSE NULL END) as TherapyPer_2016_06,
SUM(CASE when (MonthEnd='2016-06') then NonTHEp ELSE NULL END) as NonTHEp_2016_06 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-07') then 1 ELSE NULL END) as SOCCount_2016_07,
COUNT(CASE when (Recert = '1' and MonthStart='2016-07') then 1 ELSE NULL END) as RecertCount_2016_07,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-07') then 1 ELSE NULL END)) as RecertPer1_2016_07,
COUNT(CASE when (MonthStart='2016-07') then 1 ELSE NULL END) as TotalEpisode_2016_07,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-07') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_07,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-07') then CMW ELSE NULL END),3) as AvgCMW_2016_07,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-07') then 1 ELSE NULL END) as LUPAs_2016_07,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-07') then 1 ELSE NULL END) as LUPAPer_2016_07,
COUNT(CASE when ((MonthEnd='2016-07' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_07,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-07') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_07,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-07') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_07,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-07') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_07,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-07') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_07,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-07') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_07,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-07') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_07,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-07') then 1 ELSE NULL END) as Lupa_Closed_2016_07,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-07') then TH_Ep ELSE NULL END) as TherapyPer_2016_07,
SUM(CASE when (MonthEnd='2016-07') then NonTHEp ELSE NULL END) as NonTHEp_2016_07 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-08') then 1 ELSE NULL END) as SOCCount_2016_08,
COUNT(CASE when (Recert = '1' and MonthStart='2016-08') then 1 ELSE NULL END) as RecertCount_2016_08,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-08') then 1 ELSE NULL END)) as RecertPer1_2016_08,
COUNT(CASE when (MonthStart='2016-08') then 1 ELSE NULL END) as TotalEpisode_2016_08,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-08') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_08,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-08') then CMW ELSE NULL END),3) as AvgCMW_2016_08,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-08') then 1 ELSE NULL END) as LUPAs_2016_08,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-08') then 1 ELSE NULL END) as LUPAPer_2016_08,
COUNT(CASE when ((MonthEnd='2016-08' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_08,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-08') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_08,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-08') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_08,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-08') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_08,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-08') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_08,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-08') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_08,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-08') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_08,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-08') then 1 ELSE NULL END) as Lupa_Closed_2016_08,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-08') then TH_Ep ELSE NULL END) as TherapyPer_2016_08,
SUM(CASE when (MonthEnd='2016-08') then NonTHEp ELSE NULL END) as NonTHEp_2016_08 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-09') then 1 ELSE NULL END) as SOCCount_2016_09,
COUNT(CASE when (Recert = '1' and MonthStart='2016-09') then 1 ELSE NULL END) as RecertCount_2016_09,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-09') then 1 ELSE NULL END)) as RecertPer1_2016_09,
COUNT(CASE when (MonthStart='2016-09') then 1 ELSE NULL END) as TotalEpisode_2016_09,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-09') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_09,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-09') then CMW ELSE NULL END),3) as AvgCMW_2016_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-09') then 1 ELSE NULL END) as LUPAs_2016_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-09') then 1 ELSE NULL END) as LUPAPer_2016_09,
COUNT(CASE when ((MonthEnd='2016-09' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_09,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-09') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_09,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-09') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-09') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-09') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-09') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_09,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-09') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_09,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-09') then 1 ELSE NULL END) as Lupa_Closed_2016_09,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-09') then TH_Ep ELSE NULL END) as TherapyPer_2016_09,
SUM(CASE when (MonthEnd='2016-09') then NonTHEp ELSE NULL END) as NonTHEp_2016_09 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-10') then 1 ELSE NULL END) as SOCCount_2016_10,
COUNT(CASE when (Recert = '1' and MonthStart='2016-10') then 1 ELSE NULL END) as RecertCount_2016_10,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-10') then 1 ELSE NULL END)) as RecertPer1_2016_10,
COUNT(CASE when (MonthStart='2016-10') then 1 ELSE NULL END) as TotalEpisode_2016_10,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-10') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_10,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-10') then CMW ELSE NULL END),3) as AvgCMW_2016_10,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-10') then 1 ELSE NULL END) as LUPAs_2016_10,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-10') then 1 ELSE NULL END) as LUPAPer_2016_10,
COUNT(CASE when ((MonthEnd='2016-10' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_10,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-10') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_10,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-10') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_10,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-10') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_10,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-10') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_10,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-10') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_10,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-10') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_10,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-10') then 1 ELSE NULL END) as Lupa_Closed_2016_10,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-10') then TH_Ep ELSE NULL END) as TherapyPer_2016_10,
SUM(CASE when (MonthEnd='2016-10') then NonTHEp ELSE NULL END) as NonTHEp_2016_10 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-11') then 1 ELSE NULL END) as SOCCount_2016_11,
COUNT(CASE when (Recert = '1' and MonthStart='2016-11') then 1 ELSE NULL END) as RecertCount_2016_11,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-11') then 1 ELSE NULL END)) as RecertPer1_2016_11,
COUNT(CASE when (MonthStart='2016-11') then 1 ELSE NULL END) as TotalEpisode_2016_11,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-11') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_11,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-11') then CMW ELSE NULL END),3) as AvgCMW_2016_11,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-11') then 1 ELSE NULL END) as LUPAs_2016_11,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-11') then 1 ELSE NULL END) as LUPAPer_2016_11,
COUNT(CASE when ((MonthEnd='2016-11' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_11,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-11') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_11,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-11') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_11,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-11') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_11,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-11') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_11,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-11') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_11,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-11') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_11,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-11') then 1 ELSE NULL END) as Lupa_Closed_2016_11,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-11') then TH_Ep ELSE NULL END) as TherapyPer_2016_11,
SUM(CASE when (MonthEnd='2016-11') then NonTHEp ELSE NULL END) as NonTHEp_2016_11 ,
COUNT(CASE when (SOC = '1' and MonthStart='2016-12') then 1 ELSE NULL END) as SOCCount_2016_12,
COUNT(CASE when (Recert = '1' and MonthStart='2016-12') then 1 ELSE NULL END) as RecertCount_2016_12,
(COUNT(CASE when (Recert = '1' and MonthStart='2016-12') then 1 ELSE NULL END)) as RecertPer1_2016_12,
COUNT(CASE when (MonthStart='2016-12') then 1 ELSE NULL END) as TotalEpisode_2016_12,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2016-12') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2016_12,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2016-12') then CMW ELSE NULL END),3) as AvgCMW_2016_12,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-12') then 1 ELSE NULL END) as LUPAs_2016_12,
COUNT(CASE when (LUPA = '1' and MonthEnd='2016-12') then 1 ELSE NULL END) as LUPAPer_2016_12,
COUNT(CASE when ((MonthEnd='2016-12' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2016_12,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2016-12') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2016_12,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2016-12') then THVisits ELSE NULL END),1) as THVisitsTHEps_2016_12,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-12') then TotVisits ELSE NULL END),1) as AvgTotVisits_2016_12,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-12') then SNVisits ELSE NULL END),1) as AvgSNVisits_2016_12,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2016-12') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2016_12,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2016-12') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2016_12,
COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2016-12') then 1 ELSE NULL END) as Lupa_Closed_2016_12,
SUM(CASE when (Status = 'Closed' and MonthEnd='2016-12') then TH_Ep ELSE NULL END) as TherapyPer_2016_12,
SUM(CASE when (MonthEnd='2016-12') then NonTHEp ELSE NULL END) as NonTHEp_2016_12 ,
COUNT(CASE when (SOC = '1' and MonthStart='2017-01') then 1 ELSE NULL END) as SOCCount_2017_01,
COUNT(CASE when (Recert = '1' and MonthStart='2017-01') then 1 ELSE NULL END) as RecertCount_2017_01,
(COUNT(CASE when (Recert = '1' and MonthStart='2017-01') then 1 ELSE NULL END)) as RecertPer1_2017_01,
COUNT(CASE when (MonthStart='2017-01') then 1 ELSE NULL END) as TotalEpisode_2017_01,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-01') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_01,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-01') then CMW ELSE NULL END),3) as AvgCMW_2017_01,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-01') then 1 ELSE NULL END) as LUPAs_2017_01,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-01') then 1 ELSE NULL END) as LUPAPer_2017_01,
COUNT(CASE when ((MonthEnd='2017-01' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_01,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-01') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_01,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-01') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_01,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-01') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_01,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-01') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_01, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-01') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_01, ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-01') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_01, COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2017-01') then 1 ELSE NULL END) as Lupa_Closed_2017_01, SUM(CASE when (Status = 'Closed' and MonthEnd='2017-01') then TH_Ep ELSE NULL END) as TherapyPer_2017_01, SUM(CASE when (MonthEnd='2017-01') then NonTHEp ELSE NULL END) as NonTHEp_2017_01 ,COUNT(CASE when (SOC = '1' and MonthStart='2017-02') then 1 ELSE NULL END) as SOCCount_2017_02, COUNT(CASE when (Recert = '1' and MonthStart='2017-02') then 1 ELSE NULL END) as RecertCount_2017_02, (COUNT(CASE when (Recert = '1' and MonthStart='2017-02') then 1 ELSE NULL END)) as RecertPer1_2017_02, COUNT(CASE when (MonthStart='2017-02') then 1 ELSE NULL END) as TotalEpisode_2017_02, ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-02') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_02, ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-02') then CMW ELSE NULL END),3) as AvgCMW_2017_02, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-02') then 1 ELSE NULL END) as LUPAs_2017_02, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-02') then 1 ELSE NULL END) as LUPAPer_2017_02, COUNT(CASE when ((MonthEnd='2017-02' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_02, ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-02') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_02, ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-02') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_02, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-02') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_02, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-02') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_02, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-02') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_02, ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-02') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_02, COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2017-02') then 1 ELSE NULL END) as Lupa_Closed_2017_02, SUM(CASE when (Status = 'Closed' and MonthEnd='2017-02') then TH_Ep ELSE NULL END) as TherapyPer_2017_02, SUM(CASE when (MonthEnd='2017-02') then NonTHEp ELSE NULL END) as NonTHEp_2017_02 ,COUNT(CASE when (SOC = '1' and MonthStart='2017-03') then 1 ELSE NULL END) as SOCCount_2017_03, COUNT(CASE when (Recert = '1' and MonthStart='2017-03') then 1 ELSE NULL END) as RecertCount_2017_03, (COUNT(CASE when (Recert = '1' and MonthStart='2017-03') then 1 ELSE NULL END)) as RecertPer1_2017_03, COUNT(CASE when (MonthStart='2017-03') then 1 ELSE NULL END) as TotalEpisode_2017_03, ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-03') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_03, ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-03') then CMW ELSE NULL END),3) as AvgCMW_2017_03, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-03') then 1 ELSE NULL END) as LUPAs_2017_03, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-03') then 1 ELSE NULL END) as LUPAPer_2017_03, COUNT(CASE when ((MonthEnd='2017-03' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_03, ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-03') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_03, ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-03') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_03, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-03') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_03, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-03') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_03, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-03') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_03, ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-03') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_03, COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2017-03') then 1 ELSE NULL END) as Lupa_Closed_2017_03, SUM(CASE when (Status = 'Closed' and MonthEnd='2017-03') then TH_Ep ELSE NULL END) as TherapyPer_2017_03, SUM(CASE when (MonthEnd='2017-03') then NonTHEp ELSE NULL END) as NonTHEp_2017_03 ,COUNT(CASE when (SOC = '1' and MonthStart='2017-04') then 1 ELSE NULL END) as SOCCount_2017_04, COUNT(CASE when (Recert = '1' and MonthStart='2017-04') then 1 ELSE NULL END) as RecertCount_2017_04, (COUNT(CASE when (Recert = '1' and MonthStart='2017-04') then 1 ELSE NULL END)) as RecertPer1_2017_04, COUNT(CASE when (MonthStart='2017-04') then 1 ELSE NULL END) as TotalEpisode_2017_04, ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-04') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_04, ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-04') then CMW ELSE NULL END),3) as AvgCMW_2017_04, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-04') then 1 ELSE NULL END) as LUPAs_2017_04, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-04') then 1 ELSE NULL END) as LUPAPer_2017_04, COUNT(CASE when ((MonthEnd='2017-04' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_04, ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-04') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_04, ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-04') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_04, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-04') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_04, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-04') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_04, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-04') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_04, ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-04') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_04, COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2017-04') then 1 ELSE NULL END) as Lupa_Closed_2017_04, SUM(CASE when (Status = 'Closed' and MonthEnd='2017-04') then TH_Ep ELSE NULL END) as TherapyPer_2017_04, SUM(CASE when (MonthEnd='2017-04') then NonTHEp ELSE NULL END) as NonTHEp_2017_04 ,COUNT(CASE when (SOC = '1' and MonthStart='2017-05') then 1 ELSE NULL END) as SOCCount_2017_05, COUNT(CASE when (Recert = '1' and MonthStart='2017-05') then 1 ELSE NULL END) as RecertCount_2017_05, (COUNT(CASE when (Recert = '1' and MonthStart='2017-05') then 1 ELSE NULL END)) as RecertPer1_2017_05, COUNT(CASE when (MonthStart='2017-05') then 1 ELSE NULL END) as TotalEpisode_2017_05, ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-05') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_05, ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-05') then CMW ELSE NULL END),3) as AvgCMW_2017_05, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-05') then 1 ELSE NULL END) as LUPAs_2017_05, COUNT(CASE when (LUPA = '1' and MonthEnd='2017-05') then 1 ELSE NULL END) as LUPAPer_2017_05, COUNT(CASE when ((MonthEnd='2017-05' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_05, ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-05') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_05, ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-05') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_05, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-05') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_05, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-05') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_05, ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-05') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_05, ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-05') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_05, COUNT(CASE when (Status = 'Closed' and LUPA = '0' and MonthEnd='2017-05') then 1 ELSE NULL END) as Lupa_Closed_2017_05, SUM(CASE when (Status = 'Closed' and MonthEnd='2017-05') then TH_Ep ELSE NULL END) as TherapyPer_2017_05, SUM(CASE when (MonthEnd='2017-05') then NonTHEp ELSE NULL END) as NonTHEp_2017_05
FROM tb_Episode
WHERE CustID = 27 AND PayerType = 'EP' AND BranchID IN (238,239,240,241)