Is this what you are looking for ?
with MYcte as (
select studentUID, qual, max(regyear) as throughYear
from #AAA_Mytable
group by studentUID, qual
)
SELECTt1.termCalendarID, t1.regyear, t1.studentUid, t1.qual
,coalesce(oa.rollover, 0) as Rollover
from #AAA_Mytable as t1
outer apply (
SELECT 1 as rollover
frommyCTE
WHEREmycte.studentuid=t1.studentuid
and mycte.throughYear=t1.regyear-1
) as oa
This is portable to older SQL Server systems not yet running sql server 2012 and greater than or equal to 2005.
----------------------------------------------------