It is not a good approach to make all values as null and then redefining the values for that DaysActive
column.
My suggestion is to create temp table or view on top of the current table. This temp table or view will have only student id and the max value of days active.
If your table looks like this,
| StudentID | ActiveDate | DaysActive |
|-----------|------------|------------|
| AB123 | 2025-05-01 | 1 |
| AB123 | 2025-05-02 | 2 |
| AB123 | 2025-05-03 | 3 |
| AB123 | 2025-05-04 | NULL |
| BC123 | 2025-05-01 | 1 |
| BC123 | 2025-05-02 | NULL |
Then create view as below
create view vw_students_temp as
select StudentId,Max(DaysActive) as max_daysactive from Students group by StudentId
This view will give the maximum value of DaysActive for each student.
Then create a stored procedure to update only new rows.
with cte as
(Select *,row_number() over (partition by Studentid order by ActiveDate) as temp_daysActive from Students
Where daysActive is null)
update Students
set DaysActive=temp_daysActive+max_daysactive
from Students
join cte on Students.StudentID=cte.StudentID and Students.ActiveDate=cte.ActiveDate
join vw_students_temp on Students.StudentID=vw_students_temp.StudentID
Use this update statement in stored procedure. This will update only the new incoming rows and thus it will reduce the processing time.