• You should get 8 on both queries. A MAX() on a ROW_NUMBER() without PARTITION BY should give you the same values as COUNT(*).

    The reason is that you have keyids 1 and 8 repeated. You could check it with a simple query.

    SELECT *

    from @a a

    left outer join @b-2 b

    on a.keyid = b.keyid

    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