This is MSSQL forum, so MSSQL way would be:select accmanid ,custid ,total from (select accmanid ,custid ,sum(billone+billtwo) as total ,row_number() over (partition by accmanid order by accmanid,sum(billone+billtwo) desc) as rn from customers as c group by accmanid ,custid ) as c where rn<=5 order by accmanid ,total
A bit more difficult on MySQL:select accmanid ,custid ,total from (select accmanid ,custid ,total ,@rn:=if(@prev_accmanid=accmanid,@rn+1,1) as rn ,@prev_accmanid:=accmanid from (select accmanid ,custid ,sum(billone+billtwo) as total from customers group by accmanid ,custid ) as c ,(select @rn:=1) as x ,(select @prev_accmanid:=0) as y order by accmanid ,total desc ) as c where rn<=5 order by accmanid ,total desc