with data as(
select t1.tcustomerid,min(tcustomer_receptid)tcustomer_receptid,MIN(fdate)fdate from DSBCRM..tcustomer_reception t1
left join DSBCRM..tstore 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.ftype='½Ó´ý'and t1.siteid=$siteid$ and
convert(varchar(7),t1.fdate,120)>=$fbegmonth$ and convert(varchar(7),t1.fdate,120)<=$fendmonth$
and not exists(select *from tcustomer_reception
                where siteid=t1.siteid and tagentsid=t1.tagentsid
                and tcustomerid=t1.tcustomerid and ftype='ÏúÊÛ')
group by t1.tcustomerid
),
data2 as(
	select ROW_NUMBER()over(order by data.fdate)queryrow,data.* from data
),
a as(
select *from data2 t1
inner join (
	select count(*)total,CEILING(count(*)/($pageSize$*1.0)) pageTotal from data2
	) t2 on 1=1
where queryrow>($pageNumber$-1)*$pageSize$ and queryrow<=$pageNumber$*$pageSize$
)

select t1.tcustomerid,t1.fdate,
t2.fname,t2.fphonenumber,t2.fcommunity,t2.fnumber,t2.freceptiontimes,t3.fcustaction,t3.fdate as flastreceptiondate,t1.pageTotal,t1.total,
t4.fprovince,t4.fcity,t4.fcounty,t4.fagentname,t5.fname as fsaler
from a t1
inner join DSBCRM..tcustomer t2 on t1.tcustomerid=t2.tcustomerid
inner join DSBCRM..tcustomer_reception t3 on t3.siteid=t2.siteid and t3.tagentsid=t2.tagentsid and t3.tcustomerid=t2.tcustomerid and t3.tcustomer_receptid=t1.tcustomer_receptid
inner join DSBCRM..tagents t4 on t2.siteid=t4.siteid and t2.tagentsid=t4.tagentsid
left join DSBCRM..tenterprise_users t5 on t4.siteid=t5.siteid and t4.fsaleruserid=t5.tenterprise_userid
where t2.siteid=$siteid$
order by t1.fdate desc