Update data based on previous end date

Raj0125 511 Reputation points
2023-02-14T07:15:51.5266667+00:00

Hi,

I am looking to get the date update dates as below.

Update Effective StartDate + 4 seconds with previous EffectiveEnd_Date.

Sample data below.

User's image

Please suggest how to write update statment based on EffectiveEndDate.

Azure SQL Database
SQL Server | Other
{count} votes

Accepted answer
  1. LiHongMSFT-4306 31,571 Reputation points
    2023-02-15T01:37:53.0133333+00:00

    Hi @Raj0125

    Only issue with Date sould not consider previous EffectiveEndDate when Rep_Id change.

    Adding PARTITION BY in the OVER clause of LAG() function would solve this issue.

    The code is as below:

    CREATE TABLE #Temp (Skey INT,Rep_Id int,Effective_Start_Date DATETIME,Effective_End_Date DATETIME)
    INSERT INTO #Temp VALUES
    (1,11,'2021-09-03 02:31:52','2021-09-05 02:30:54'),
    (2,11,'2021-09-05 02:30:59','2021-09-07 02:31:38'),
    (3,11,'2021-09-07 02:31:43','2021-09-08 02:51:14'),
    (4,11,'2021-09-08 02:51:19','2021-09-14 02:38:53'),
    (5,12,'2021-09-03 02:31:52','2021-09-05 02:30:54'),
    (6,12,'2021-09-05 02:30:59','2021-09-07 02:31:38'),
    (7,12,'2021-09-07 02:31:43','2021-09-08 02:51:14'),
    (8,12,'2021-09-08 02:51:19','2021-09-14 02:38:53')
    
    ;WITH CTE AS
    (
     SELECT Skey,Effective_Start_Date,Effective_End_Date,
            DATEADD(SECOND,4,LAG(Effective_End_Date)OVER(PARTITION BY Rep_Id ORDER BY Skey)) AS New_Effective_Start_Date
     FROM #Temp
    )
    UPDATE CTE
    SET Effective_Start_Date = New_Effective_Start_Date
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. LiHongMSFT-4306 31,571 Reputation points
    2023-02-14T07:37:43.91+00:00

    Hi @Raj0125

    Try this:

    CREATE TABLE #Temp (Skey INT,Effective_Start_Date DATETIME,Effective_End_Date DATETIME)
    INSERT INTO #Temp VALUES
    (1,'2021-09-03 02:31:52','2021-09-05 02:30:54'),
    (2,'2021-09-05 02:30:59','2021-09-07 02:31:38'),
    (3,'2021-09-07 02:31:43','2021-09-08 02:51:14'),
    (4,'2021-09-08 02:51:19','2021-09-14 02:38:53')
    
    ;WITH CTE AS
    (
     SELECT Skey,Effective_Start_Date,Effective_End_Date,
            DATEADD(SECOND,4,LAG(Effective_End_Date)OVER(ORDER BY Skey)) AS New_Effective_Start_Date
     FROM #Temp
    )
    UPDATE CTE
    SET Effective_Start_Date = New_Effective_Start_Date
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

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.