Share via

Update record from previous Effec_strt_date

Raj0125 511 Reputation points
2023-05-22T14:46:34.7566667+00:00

Hi,

Below is the sample data its fetching.

User's image

I want to chnage the data like below.First record of Effectiver_start_date ,Inserted_Date ,Updated_Date values should be from below record Effective_Start_Date value where Current_Ind ='N' for each S_id

User's image

Please advice.

Thanks in advance.

Azure SQL Database
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

LiHongMSFT-4306 31,621 Reputation points
2023-05-23T01:41:30.36+00:00

Hi @Raj0125

Try this query:

;WITH CTE AS
(
 SELECT *,MAX(CASE WHEN Current_Ind='N' THEN Effective_Start_Date ELSE NULL END)OVER(PARTITION BY S_Id)AS New_Value
 FROM TableName 
)
UPDATE CTE SET Effective_Start_Date=New_Value, Inserted_Date=New_Value, Updated_Date=New_Value
WHERE Current_Ind='Y'

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.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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