select pid from (select pid,ROW_NUMBER() over (order by sum(amount) ) s1,ROW_NUMBER() over (order by sum(amount) desc) s2 from pro group by pid)t where s1<=3 or s2<=3