select case when t2.ftype='主项'then t2.fcostname else'其他' end fcostname,sum(t2.famount)famount from tagents_cost t1
inner join tagents_costmx t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid and t1.tagents_costid=t2.tagents_costid
where t1.siteid=$siteid$ and t1.tagentsid=$tagentsid$ and t2.fcostname!='薪资提成' and
convert(varchar(7),t1.fdate,120)>=$fbegmonth$ and convert(varchar(7),t1.fdate,120)<=$fendmonth$
group by case when t2.ftype='主项'then t2.fcostname else'其他' end
order by sum(t2.famount) desc
