SQL Query logic

Santosh Kumar 61 Reputation points
2022-03-15T06:01:01.413+00:00

Hi There,

I have a requirement with the source data as shown below. At Customer ID level based on Date ascender order, i need to populate the min value for all null records. Could you plz help me with the logic.

183182-capture3.png
Thanks in advance.
Santosh S

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. LiHong-MSFT 10,056 Reputation points
    2022-03-15T06:49:29.153+00:00

    Hi @Santosh Kumar
    Please check this :

    ;WITH CTE AS  
    (SELECT T.*,C.Region AS New_Region   
     FROM Your_table  T CROSS APPLY(SELECT TOP(1)Region FROM Your_table   
                                    WHERE Region IS NOT NULL AND custumerID=T.custumerID   
                                    ORDER BY Date)C  
    )--SELECT * FROM CTE WHERE Region IS NULL  
    UPDATE CTE   
    SET Region=New_Region  
    WHERE Region IS NULL  
    

    Best regards,
    LiHong


    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.

    0 comments No comments

0 additional answers

Sort by: Most 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.