select t.fmonth,sum(t.famount)famount  from (
select convert(varchar(7),t1.fdate,120)as fmonth,-sum(t2.famount)famount from DSBCRM..tagents_cost t1
inner join DSBCRM..tagents_costmx t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid and t1.tagents_costid=t2.tagents_costid
inner join DSBCRM..tagents t3 on t1.siteid=t3.siteid and t1.tagentsid=t3.tagentsid
where t1.siteid=$siteid$ and
convert(varchar(7),t1.fdate,120)>=$fbegmonth$ and convert(varchar(7),t1.fdate,120)<=$fendmonth$
and t3.fsaleruserid in $tenterprise_userid$
group by convert(varchar(7),t1.fdate,120),t1.fmonth
union all
select convert(varchar(7),t1.fdate,120)as fmonth,SUM(t1.forderamount)famount from DSBCRM..tcustomer_reception t1
inner join DSBCRM..tagents t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid
where t1.ftype='ÏúÊÛ' and t1.siteid=$siteid$ and
convert(varchar(7),t1.fdate,120)>=$fbegmonth$ and convert(varchar(7),t1.fdate,120)<=$fendmonth$
and t2.fsaleruserid in $tenterprise_userid$
group by convert(varchar(7),t1.fdate,120)
)t group by t.fmonth