select tt.*,
    case
        when tt.frebuilddate < DATEADD(year,-2,getdate()) then '<3Äê'
        when tt.frebuilddate < DATEADD(year,-1,getdate()) then '<2Äê'
        else '<1Äê' end fclass
from (
                 select t3.fagentshortname,
                        t1.fstorename,
                        t1.fstartdate,
                        min(t2.fdate)                frebuilddate,
                        count(t2.tstore_rebuildloid) fcount,
                        avg(t2.fintervaltime/30)       fintervaltime
                 from DSBCRM..tstore t1
                          inner join DSBCRM..tstore_rebuildlog t2
                                     on t1.siteid = t2.siteid and t1.tagentsid = t2.tagentsid and
                                        t1.tstoreid = t2.tstoreid
                          inner join DSBCRM..tagents t3 on t1.siteid = t3.siteid and t1.tagentsid = t3.tagentsid
                 where t1.siteid =$siteid$ and t3.fsaleruserid in $tenterprise_userid$
                 group by t3.fagentshortname, t1.fstorename, t1.fstartdate
             )tt