select 分公司,地区,salary,rank from (
select heyf_tmp.分公司,heyf_tmp.地区,heyf_tmp.salary,@rownum:=@rownum+1 ,
if(@pdept=heyf_tmp.分公司,@rank:=@rank+1,@rank:=1) as rank,
@pdept:=heyf_tmp.分公司
from (
select count(1) empid,
substring_index(c.duty_third_org_name, '_',- 1 ) 分公司,
c.second_division 地区,
sum(cs.bid_amt) salary
from cscec8b_project_info_ver c
left join cscec8b_project_attr_info_ver cs ON c.row_id = cs.row_id
group by substring_index(c.duty_third_org_name, '_',- 1 ),c.second_division
order by substring_index(c.duty_third_org_name, '_',- 1 ) asc,sum(cs.bid_amt) desc
) heyf_tmp ,(select @rownum :=0 , @pdept := null ,@rank:=0) a ) result
where rank<=3