• The left join is odd indeed. Without allowing for NULLs in the join criteria, I'd imagine that the LEFT join behavior is being overridden to INNER join.(edit: removed the silly comment after rereading the query)

    I'd consider aggregating the values in b separately and the values in a separately then comparing them (since you're apparently looking for the highest sequence in a as long as the a sequences are higher than in b)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?