select *
from (select t.*,
row_number() over(order by t.CONTRACTNOFILEID desc) rn
from ly_publicrental_contractinfo t
where Reconciliation = Valid
and (contractno = varContractNo or varContractNo is null))
where rn between (pageIndex - 1) * pageSize and pageIndex * pageSize;