Iam Not Sure what output you want.
it seems your SP is very complex .If you could give me more detail and i can try to solve more.
but i think you need to change your query like this.
chk with this hope this will be help you to work out more on your query.
Alter Procedure Forms9(@mdvn int,@mphc varchar(4),@mhsc varchar(2), @fdate date, @tdate date)
as*
BEGIN
declare @fedddate date
declare @tedddate date
declare @v1date date
declare @v2date date
declare @v3date date
set @v1date=dateadd(dd,-190,@fdate)
set @v2date=dateadd(dd,-92,@fdate)
set @v3date=dateadd(dd,-45,@fdate)
set @fedddate=dateadd(dd,-320,@fdate)
set @tedddate=dateadd(dd,30,@tdate)
SELECT DVN_CD,PHC_CD,HSC_CD,SUM(REGDT) AS REGDT,SUM(trims1)AS Trim1,sum(trims2) AS Trim2,
sum(ABORT) AS ABORT,sum(MTB) AS MTB,sum(LB)AS LB,sum(SB) AS SB,SUM(ANC1) as ANC1,SUM(ANC2) as ANC2,SUM(ANC3) as ANC3,
SUM(TT1) as TT1,sum(TTB2) as TT2,sum(IFA1) as IFA,sum(BP) as BP,sum(HB1) as HB FROM
(
SELECT a.DVN_CD,a.PHC_CD,a.HSC_CD,
CASE WHEN AN_Reg_Date is not null THEN 1 ELSE 0 END AS REGDT,
CASE WHEN DATEDIFF(DD,lmp,an_reg_date)<=90 AND AN_Reg_Date is not null THEN 1 ELSE 0 END AS trims1,
CASE WHEN DATEDIFF(DD,lmp,an_reg_date)>90 AND AN_Reg_Date is not null THEN 1 ELSE 0 END AS trims2,
CASE WHEN Preg_outcome=1 AND Deli_Date is not null THEN 1 ELSE 0 END AS ABORT,
CASE WHEN Preg_outcome=2 AND Deli_Date is not null THEN 1 ELSE 0 END AS MTB,
CASE WHEN Preg_outcome=3 AND Deli_Date is not null THEN 1 ELSE 0 END AS LB,
CASE WHEN Preg_outcome=4 AND Deli_Date is not null THEN 1 ELSE 0 END AS SB,
case when visit_date is not null then 1 else 0 end as ANC1,
case when visit_date is not nullthen 1 else 0 end as ANC2,
case when visit_date is not null then 1 else 0 end as ANC3,
case when TTB=1 and TTDate is not null then 1 else 0 end as TT1,
case when TTB>=2 and TTDate is not null then 1 else 0 end as TTB2,
case when IFA=100 and IFADate is not null then 1 else 0 end as IFA1,
case when BP>='140/90' and ANEDDis not null then 1 else 0 end as BP,
case when HB<11 and ANEDD is not null then 1 else 0 end as HB1
FROM Preg_cohort3 a
Inner join ANVisits3 b
on a.DVN_CD=b.DVN_CD and a.PHC_CD=b.PHC_CD and a.HSC_CD=b.HSC_CD
Where
a.DVN_CD=@mdvn and a.PHC_CD=@mphc and a.HSC_CD=@mhsc
and AN_Reg_Date BETWEEN @fdate AND @tdate
)a group by a.dvn_cd,a.phc_cd,a.hsc_cd
RETURN
END