select t.fprovince,
       t.fcity,
       t.fcounty,
       t.fagentnum,
       t.fagentname,
       t.famount,
       t.famount/12 as faverageamount,
       t.flastyearamount,
       case when t.flastyearamount = 0 then 0 else (t.famount - t.flastyearamount)*100 / t.flastyearamount end as fgrowthrate
from (
         select t2.fprovince,
                t2.fcity,
                t2.fcounty,
                t2.fagentnum,
                t2.fagentname,
                sum(case when t1.fyear = $fyear$ then t1.famount else 0 end)  as famount,
                sum(case when t1.fyear != $fyear$ then t1.famount else 0 end) as flastyearamount
         from DSBCRM..tagents_purchase t1
                  inner join DSBCRM..tagents t2 on t1.siteid = t2.siteid and t1.tagentsid = t2.tagentsid
         where t1.fyear in ($fyear$, $fyear$ - 1)
           and t1.fmonth in ($fmonth$)
           and t1.siteid =$siteid$
           and t2.fsaleruserid in $tenterprise_userid$
         group by t2.fprovince, t2.fcity, t2.fcounty, t2.fagentnum, t2.fagentname
     ) t