Keeping a counter and increment it via ADF

sam nick 306 Reputation points
2025-04-28T14:41:44.9+00:00

Hello,

I currently have the below data and for the column of Days Active, it is a daily counter that keeps incrementing. I currently employ the methos of nullifying the column and them executing a stored proc. but this process takes around 60 mins to be done.

I tried using ADF too by using a window function on the studentid (also running a pre sql script of nullifying that field). Using this option too, takes 1 hour. The main table has around 3 Million rows

Any recommendations on how to implement this effectively please.

Day1:

User's image

Day 2:

User's image

Day 3:

User's image

Thank you.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,464 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Aswin 392 Reputation points
    2025-05-02T09:01:35.2566667+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.