select x.*,
    ceil(rnum / :lcnt) as pgno,
    max(rnum) over() as maxnum
from (
    select z.*,
	ROW_NUMBER() over (order by z.cls,z.scls1,z.scls2,z.scls3,z.scls4) as rnum
    from ( 
        select cls, scls1, scls2, scls3, scls4, scls5,
                scls6, scls7, scls8, scls9,
                snum1, snum2, snum3,
                nm, nm2, nm3, nm4
        from afm90
        where (:cls is null or cls=:cls)
          and (:cls<>'5' or :cls='5' and (:bknm is null or (comnpack.MultiTOSingleBytesEx(nm))
                                        like '%'||upper(comnpack.MultiTOSingleBytesEx(:bknm))||'%' or 
                                        upper(comnpack.MultiTOSingleBytesEx(nm3))
                                        like '%'||upper(comnpack.MultiTOSingleBytesEx(:bknm))||'%' )
                and :cls='5' and (:bksnm is null or (comnpack.MultiTOSingleBytesEx(nm2))
                                        like '%'||upper(comnpack.MultiTOSingleBytesEx(:bksnm))||'%' or 
                                        upper(comnpack.MultiTOSingleBytesEx(nm4))
                                        like '%'||upper(comnpack.MultiTOSingleBytesEx(:bksnm))||'%' ) )
        order by cls, scls1, scls2, scls3, scls4
    ) z
) x