with data as(
select t1.tcustomerid,min(tcustomer_receptid)tcustomer_receptid,MIN(fdate)fdate from tcustomer_reception t1
left join tstore t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid and t1.tstoreid=t2.tstoreid
where t1.ftype='½Ó´ý'and t1.siteid=$siteid$ and t1.tagentsid=$tagentsid$ 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
from a t1
inner join tcustomer t2 on t1.tcustomerid=t2.tcustomerid
inner join 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
where t2.siteid=$siteid$ and t2.tagentsid=$tagentsid$
order by t1.fdate desc