SELECT t1.OrderId,t1.QueueId
FROM (
SELECT tq.OrderId,tq.QueueId,ROW_NUMBER() OVER(PARTITION BY tq.OrderId ORDER BY tq.QueueId DESC) rn
FROM TransferQueue tq WITH(NOLOCK)
) t1
WHERE rn = 1
FROM (
SELECT tq.OrderId,tq.QueueId,ROW_NUMBER() OVER(PARTITION BY tq.OrderId ORDER BY tq.QueueId DESC) rn
FROM TransferQueue tq WITH(NOLOCK)
) t1
WHERE rn = 1