USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[func2]
(@mdvn int,@fdate date,@tdate date)
returns @funcurz2 table
(
mdv int, fdate date, tdate date,ANC int,
TT1 int,IFA int,BP int,HB int
)
as
begin
declare @vdate date
declare @tvdate date
declare @fedddate date
declare @tedddate date
set @vdate=dateadd(dd,-91,@fdate)
set @tvdate=dateadd(dd,36,@tdate)
set @fedddate=dateadd(dd,20,@fdate)
set @tedddate=dateadd(dd,310,@tdate)
declare @mdv int
declare @cfdate date
declare @ctdate date
declare @ANC int
declare @TT1 int
declare @IFA int
declare @BP int
declare @HB int
declare funcurz cursor static for
select dvn_cd, sum(REGDT) as Regdt,SUM(ANC) as ANC,SUM(TT1) as TT1, SUM(TT2)as TT2,
SUM(IFA) as IFA, SUM(BP1) as BP1, SUM(HB1) as HB1 from
(
select dvn_cd,phc_cd,hsc_cd,Visit_Date,TTDate,anc_fullId,TTB,IFADate,BP,HB,
case when ANEDD IS null then 0 else 1 end as REGDT,
case when visit_no=3 and ANEDD between @vdate and @tvdate then 1 else 0 end as ANC,
case when TTB=1 and ANEDD between @fdate and @tdate then 1 else 0 end as TT1,
case when TTB>2 and ANEDD between @fdate and @tdate then 1 else 0 end as TT2,
case when IFA=100 and ANEDD between @fdate and @tdate then 1 else 0 end as IFA,
case when BP>='140/90' and ANEDD between @fdate and @tdate then 1 else 0 end as BP1,
case when HB<11 and ANEDD between @fdate and @tdate then 1 else 0 end as HB1
from ANVisits3 a where DVN_CD=@mdvn and ANEDD between @fedddate and @tedddate
)a group by dvn_cd,phc_cd,hsc_cd
open funcurz
begin
fetch next from funcurz into @mdv,@ANC,@TT1,@IFA,@BP,@HB
while @@FETCH_STATUS=0
begin
insert into @funcurz2(@mdv,@cfdate,@ctdate,@ANC,@TT1,@TT2,@IFA,@BP,@HB)
fetch next from funcurz into @mdv,@ANC,@TT1,@IFA,@BP,@HB
END
end
close funcurz
deallocate funcurz
return
end