select * from (select * from attachments order by workorderid,LastUpdated desc) t group by workorderid;