• Here's a more concise way to do it. You can update the table by updating the CTE.;-)

    WITH CTE AS(

    SELECT *, ROW_NUMBER() OVER( PARTITION BY ID ORDER BY ltr) rn

    FROM #tmp

    )

    UPDATE CTE SET

    flag = 'Y'

    WHERE rn = 1;

    If your rank is not ordered alphabetically, you can simply change the column with the CASE statement in the ORDER BY clause

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2