with data as(
select t1.tcustomerid,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$
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.fbirthdate,t2.fwechatno,t2.fcommunity,t2.fnumber,
  sum(t3.forderamount)forderamount,
  sum(t3.fdepositamount)fdepositamount,
  sum(t3.freceivedamount)freceivedamount,
  sum(t3.freceivablesamount)freceivablesamount
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.ftype='ÏúÊÛ'
where t2.siteid=$siteid$ and t2.tagentsid=$tagentsid$
group by t1.tcustomerid,t1.fdate,t2.fname,t2.fphonenumber,t2.fbirthdate,t2.fwechatno,t2.fcommunity,t2.fnumber
order by t1.fdate desc