select t1.tagentactivitiesid,
       t1.ftitle,--标题
       t1.fnotes,
       t1.fphonenumber,
       t1.fbegdate,--开始日期
       t1.fenddate,--结束日期
       t1.fstatus,--活动状态
       t1.fupperlimitamount,--预算
       isnull(t1.fupperlimitamount, 0) - isnull(t1.fusedamount, 0) as flessamount,--剩余预算
       SUM(t2.fvisitors)                                              freadcount,--浏览次数
       count(distinct t3.tagentactivities_shareid)                             fsharecount,--分享次数
       count(distinct t2.tagentactivities_custid)                              fcustcount,--流量，阅读人数
       count(distinct t4.tagentactivities_formid)                              fformcount--线索，意向客户数
from tagentactivities t1
         left join tagentactivities_cust t2 on t1.siteid = t2.siteid and t1.tagentactivitiesid = t2.tagentactivitiesid
         left join tagentactivities_share t3 on t1.siteid = t3.siteid and t1.tagentactivitiesid = t3.tagentactivitiesid
         left join tagentactivities_form t4 on t1.siteid = t4.siteid and t1.tagentactivitiesid = t4.tagentactivitiesid
where t1.siteid =$siteid$ and t1.tagentactivitiesid=$tagentactivitiesid$
group by t1.tagentactivitiesid,
    t1.ftitle,
    t1.fnotes,
    t1.fphonenumber,
    t1.fbegdate,
    t1.fenddate,
    t1.fstatus,
    t1.fupperlimitamount,
    isnull(t1.fupperlimitamount, 0)-isnull(t1.fusedamount, 0)