here is the query, all @ have been replaced with $:
declare $ContractStart1 date,
$ContractEnd1 date,
$PaidDateStart1 date,
$PaidDateEnd1 date,
$ContractStart2 date,
$ContractEnd2 date,
$PaidDateStart2 date,
$PaidDateEnd2 date,
$ContractStart3 date,
$ContractEnd3 date,
$PaidDateStart3 date,
$PaidDateEnd3 date,
$HVMA2012 money,
$NonHVMA2012 money,
$HVMA2013 money,
$NonHVMA2013 money,
$HVMA2014 money,
$NonHVMA2014 money,
$HVMA2015 money,
$NonHVMA2015 money,
$HVMA2016 money,
$NonHVMA2016 money
set $ContractStart1 = '04/01/2011'
set $ContractEnd1 = '03/31/2012'
set $PaidDateStart1 = '04/01/2011'
set $PaidDateEnd1 = '04/30/2015'
set $ContractStart2 = '04/01/2012'
set $ContractEnd2 = '03/31/2013'
set $PaidDateStart2 = '04/01/2012'
set $PaidDateEnd2 = '04/30/2015'
set $ContractStart3 = '04/01/2013'
set $ContractEnd3 = '03/31/2014'
set $PaidDateStart3 = '04/01/2013'
set $PaidDateEnd3 = '04/30/2015'
set $HVMA2012 = '95.24'
set $NonHVMA2012 = '10.42'
set $HVMA2013 = '49.36'
set $NonHVMA2013 = '7.17'
set $HVMA2014 = '90.26'
set $NonHVMA2014 = '16.52'
set $HVMA2015 = '91.74'
set $NonHVMA2015 = '16.22'
set $HVMA2016 = '91.74'
set $NonHVMA2016 = '16.22'
/Period 1/
--Members - P1
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, SUM(membermonths) as All_Mems
into #Members1
from DW..Membermonths
where ReportingMonth between $ContractStart1 and $ContractEnd1
and PolicyNumber = '044MG'
--Premium - P1
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, sum(BilledPremiumAmount) as Prem_Amt
into #Premium1
from dw..PremiumBilling
where PremiumMonth between $ContractStart1 and $ContractEnd1
and InvoiceStatus = 'Billed'
and PolicyNumber = '044MG'
--Claims and CAP - P1
--inpatient and outpatient
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, sum(cl.NHPLiability) as Med_Amt
into #IP_OP1
from dw..ClaimLine cl
inner join dw..ClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart1 and $ContractEnd1
and ch.PaidDate between $PaidDateStart1 and $PaidDateEnd1
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--mental health
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, sum(cl.NHPLiability) as MH_Amt
into #MH1
from dw..MHClaimLine cl
inner join dw..MHClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart1 and $ContractEnd1
and ch.PaidDate between $PaidDateStart1 and $PaidDateEnd1
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--pharmacy
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, sum(cl.NHPLiability) as Rx_Amt
into #Pharmacy1
from dw..PharmacyService cl
where cl.ServiceDateFrom between $ContractStart1 and $ContractEnd1
and cl.LagPaidDate between $PaidDateStart1 and $PaidDateEnd1
and cl.PolicyNumber = '044MG'
and cl.ClaimStatus in ('PAID','PAY')
and cl.ResubmittedClaimId = ''
--capitation
select $ContractStart1 as Contract_Start
, $ContractEnd1 as Contract_End
, sum(
case
when pcpnetworkid = 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$HVMA2012)
when pcpnetworkid = 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$HVMA2013)
when pcpnetworkid = 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$HVMA2014)
when pcpnetworkid = 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$HVMA2015)
when pcpnetworkid = 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$HVMA2016)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$NonHVMA2012)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$NonHVMA2013)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$NonHVMA2014)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$NonHVMA2015)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$NonHVMA2016)
end
)
as Cap_Amt
into #Cap1
from dw..membermonths
where reportingmonth between $ContractStart1 and $ContractEnd1
and PolicyNumber = '044MG'
/Period 2/
--Members - P2
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, SUM(membermonths) as All_Mems
into #Members2
from DW..Membermonths
where ReportingMonth between $ContractStart2 and $ContractEnd2
and PolicyNumber = '044MG'
--Premium - P2
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, sum(BilledPremiumAmount) as Prem_Amt
into #Premium2
from dw..PremiumBilling
where PremiumMonth between $ContractStart2 and $ContractEnd2
and InvoiceStatus = 'Billed'
and PolicyNumber = '044MG'
--Claims and CAP - P2
--inpatient and outpatient
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, sum(cl.NHPLiability) as Med_Amt
into #IP_OP2
from dw..ClaimLine cl
inner join dw..ClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart2 and $ContractEnd2
and ch.PaidDate between $PaidDateStart2 and $PaidDateEnd2
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--mental health
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, sum(cl.NHPLiability) as MH_Amt
into #MH2
from dw..MHClaimLine cl
inner join dw..MHClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart2 and $ContractEnd2
and ch.PaidDate between $PaidDateStart2 and $PaidDateEnd2
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--pharmacy
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, sum(cl.NHPLiability) as Rx_Amt
into #Pharmacy2
from dw..PharmacyService cl
where cl.ServiceDateFrom between $ContractStart2 and $ContractEnd2
and cl.LagPaidDate between $PaidDateStart2 and $PaidDateEnd2
and cl.PolicyNumber = '044MG'
and cl.ClaimStatus in ('PAID','PAY')
and cl.ResubmittedClaimId = ''
--capitation
select $ContractStart2 as Contract_Start
, $ContractEnd2 as Contract_End
, sum(
case
when pcpnetworkid = 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$HVMA2012)
when pcpnetworkid = 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$HVMA2013)
when pcpnetworkid = 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$HVMA2014)
when pcpnetworkid = 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$HVMA2015)
when pcpnetworkid = 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$HVMA2016)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$NonHVMA2012)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$NonHVMA2013)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$NonHVMA2014)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$NonHVMA2015)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$NonHVMA2016)
end
)
as Cap_Amt
into #Cap2
from dw..membermonths
where reportingmonth between $ContractStart2 and $ContractEnd2
and PolicyNumber = '044MG'
/Period 3/
--Members - P3
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, SUM(membermonths) as All_Mems
into #Members3
from DW..Membermonths
where ReportingMonth between $ContractStart3 and $ContractEnd3
and PolicyNumber = '044MG'
--Premium - P3
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, sum(BilledPremiumAmount) as Prem_Amt
into #Premium3
from dw..PremiumBilling
where PremiumMonth between $ContractStart3 and $ContractEnd3
and InvoiceStatus = 'Billed'
and PolicyNumber = '044MG'
--Claims and CAP - P3
--inpatient and outpatient
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, sum(cl.NHPLiability) as Med_Amt
into #IP_OP3
from dw..ClaimLine cl
inner join dw..ClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart3 and $ContractEnd3
and ch.PaidDate between $PaidDateStart3 and $PaidDateEnd3
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--mental health
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, sum(cl.NHPLiability) as MH_Amt
into #MH3
from dw..MHClaimLine cl
inner join dw..MHClaimHeader ch
on ch.ClaimID = cl.ClaimID
where ServiceDateFrom between $ContractStart3 and $ContractEnd3
and ch.PaidDate between $PaidDateStart3 and $PaidDateEnd3
and ch.PolicyNumber = '044MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''
--pharmacy
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, sum(cl.NHPLiability) as Rx_Amt
into #Pharmacy3
from dw..PharmacyService cl
where cl.ServiceDateFrom between $ContractStart3 and $ContractEnd3
and cl.LagPaidDate between $PaidDateStart3 and $PaidDateEnd3
and cl.PolicyNumber = '044MG'
and cl.ClaimStatus in ('PAID','PAY')
and cl.ResubmittedClaimId = ''
--capitation
select $ContractStart3 as Contract_Start
, $ContractEnd3 as Contract_End
, sum(
case
when pcpnetworkid = 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$HVMA2012)
when pcpnetworkid = 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$HVMA2013)
when pcpnetworkid = 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$HVMA2014)
when pcpnetworkid = 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$HVMA2015)
when pcpnetworkid = 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$HVMA2016)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2012' and '12/01/2012' then (membermonths*$NonHVMA2012)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2013' and '12/01/2013' then (membermonths*$NonHVMA2013)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2014' and '12/01/2014' then (membermonths*$NonHVMA2014)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2015' and '12/01/2015' then (membermonths*$NonHVMA2015)
when pcpnetworkid <> 2 and reportingmonth between '01/01/2016' and '12/01/2016' then (membermonths*$NonHVMA2016)
end
)
as Cap_Amt
into #Cap3
from dw..membermonths
where reportingmonth between $ContractStart3 and $ContractEnd3
and PolicyNumber = '044MG'
/final reports/
--Period 1
select * from #Members1
select * from #Premium1
select m.Contract_Start, m.Contract_End, (m.Med_Amt + mh.MH_Amt + rx.Rx_Amt + c.Cap_Amt) as Total
from #IP_OP1 m
inner join #MH1 mh
on m.Contract_Start = MH.Contract_Start and m.Contract_End = MH.Contract_End
inner join #Pharmacy1 rx
on m.Contract_Start = rx.Contract_Start and m.Contract_End = rx.Contract_End
inner join #Cap1 c
on m.Contract_Start = c.Contract_Start and m.Contract_End = c.Contract_End
--Period 2
select * from #Members2
select * from #Premium2
select m.Contract_Start, m.Contract_End, (m.Med_Amt + mh.MH_Amt + rx.Rx_Amt + c.Cap_Amt) as Total
from #IP_OP2 m
inner join #MH2 mh
on m.Contract_Start = MH.Contract_Start and m.Contract_End = MH.Contract_End
inner join #Pharmacy2 rx
on m.Contract_Start = rx.Contract_Start and m.Contract_End = rx.Contract_End
inner join #Cap2 c
on m.Contract_Start = c.Contract_Start and m.Contract_End = c.Contract_End
--Period 3
select * from #Members3
select * from #Premium3
select m.Contract_Start, m.Contract_End, (m.Med_Amt + mh.MH_Amt + rx.Rx_Amt + c.Cap_Amt) as Total
from #IP_OP3 m
inner join #MH3 mh
on m.Contract_Start = MH.Contract_Start and m.Contract_End = MH.Contract_End
inner join #Pharmacy3 rx
on m.Contract_Start = rx.Contract_Start and m.Contract_End = rx.Contract_End
inner join #Cap3 c
on m.Contract_Start = c.Contract_Start and m.Contract_End = c.Contract_End