select b.* from (select id,sum(v) as total_v from yourtable group by id) a left join yourtable b on a.id=b.id order by a.total_v desc,b.tm asc