This works but not sure about the performance ?...
Create procedure Test as
DECLARE @ID int,
@Salary money,
@RunningTotal money,
@GroupCount int
SET @RunningTotal = 0
SET @GroupCount=1
DECLARE rt_cursor CURSOR
FOR SELECT ID, Salary FROM employee
order by ID
select ID, SPACE(50) Grouped into #temp from employee
OPEN rt_cursor
FETCH NEXT FROM rt_cursor INTO @ID,@Salary
WHILE @@FETCH_STATUS = 0
BEGIN
if (@RunningTotal +@Salary > 5000)
Begin
SET @GroupCount=@GroupCount+1
SET @RunningTotal = @Salary
End
else
Begin
SET @RunningTotal = @RunningTotal + @Salary
End
update #temp
set Grouped='Group ' + STR(@GroupCount)
where ID=@ID
FETCH NEXT FROM rt_cursor INTO @ID,@Salary
END
CLOSE rt_cursor
DEALLOCATE rt_cursor
select * from #temp