select t1.fstoretype,t1.fmarket,t1.fstorename,t1.fphonenumber,t1.farea,count(t2.tstore_rebuildloid)frebuildcount,max(t2.fdate)frebuilddate from DSBCRM..tstore t1
left join DSBCRM..tstore_rebuildlog t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid and t1.tstoreid=t2.tstoreid
where t1.siteid=$siteid$ and t1.tagentsid=$tagentsid$
group by t1.fstoretype,t1.fmarket,t1.fstorename,t1.fphonenumber,t1.farea