#T2 contains data from post periods 11010, 11011, 11012, 11101, 11102, and 11103.
Post period 11103 does not contain any CPT codes of 96372, where the previous periods do. So, when it does the running sum, it doesn't sum the units from previous periods for that CPT code.
Any thoughts?
CODE
select PostPeriod, NPI, provider, location, location_mapping, location_category, category, CPT, modifier, multiplier, wRVU10,
MTDCharges, MTDUnits, AdjwRVU10, MTDNetwRVU10,
(select sum(isnull(b.MTDUnits,0))
from #T2 b
where b.PostPeriod<=a.PostPeriod
and b.NPI=a.NPI
and b.location=a.location
and b.category=a.category
and b.CPT=a.CPT
and b.modifier=a.modifier) as YTDUnits
from #T2 a
where PostPeriod<='11103'
group by PostPeriod, NPI, provider, location, location_category, location_mapping, category, CPT, modifier, multiplier, wRVU10,
MTDCharges, MTDUnits, AdjwRVU10, MTDNetwRVU10
MTDCharges, MTDUnits, AdjwRVU10, MTDNetwRVU10,
(select sum(isnull(b.MTDUnits,0))
from #T2 b
where b.PostPeriod<=a.PostPeriod
and b.NPI=a.NPI
and b.location=a.location
and b.category=a.category
and b.CPT=a.CPT
and b.modifier=a.modifier) as YTDUnits
from #T2 a
where PostPeriod<='11103'
group by PostPeriod, NPI, provider, location, location_category, location_mapping, category, CPT, modifier, multiplier, wRVU10,
MTDCharges, MTDUnits, AdjwRVU10, MTDNetwRVU10