select t2.tenterprise_userid,
       t3.fsaler,
       t3.fname,
       t3.fprovince,
       t3.fcity,
       t3.fcounty,
       t3.fphonenumber,
       MAX(t4.createdate) as                              flastreadtime,
       count(distinct t4.tarchives_ad_useid)              freadcount,
       sum(DATEDIFF(MINUTE, t4.createdate, t4.closedate)) freadtimes,
       count(t6.tattachment_downid)                       fdownloadtimes
from tarchives_ad t1
         inner join tenterprise_users t2 on t1.siteid = t2.siteid and t2.fphonenumber is not null
    and t1.fagentcanread = 1 and t2.fusertype = '¾­ÏúÉÌ' and t2.fisadministrator = 1
         inner join user_agent_saler_view t3 on t1.siteid = t3.siteid and t2.tenterprise_userid = t3.tenterprise_userid
         left join tarchives_ad_userdata t4
                   on t1.siteid = t4.siteid and t2.tenterprise_userid = t4.tenterprise_userid and
                      t1.tarchives_adid = t4.tarchives_adid
         inner join tattachment t5
                    on t1.siteid = t5.siteid and t5.ownertable = 'tarchives_ad' and t1.tarchives_adid = t5.ownerid
         inner join tattachment_downlog t6 on t5.siteid = t6.siteid and t5.tattachmentid = t6.tattachmentid and
                                              t6.tenterprise_userid = t2.tenterprise_userid
where t1.siteid = $siteid$ and t1.tarchives_adid=$tarchives_adid$ and t3.fsaleruserid in $tenterprise_userid$
group by t2.tenterprise_userid, t3.fsaler, t3.fname, t3.fprovince, t3.fcity, t3.fcounty, t3.fphonenumber