SELECT tbl01.nr01 as nr01
FROM 
( 

    /**************************
          tbl01=purch.records
      **************************/
      SELECT i01.nr01 as nr01, 
             i01.p013 as p013, 
             i01.remfdate as remfdate, 
             i01.remftime remftime 
      FROM i01, i15
      WHERE i01.p01='{spec_char(npno)}'
         && i01.p05=i15.p01
         && i01.p022<>0
         && i01.p03<>0
         && i01.p012=1
         && i15.p05=1
         && i01.p014<>''
      ORDER BY i01.p013 DESC, i01.remfdate desc, i01.remftime desc, i01.nr01 desc 
      limit 1
) as tbl01         
ORDER BY tbl01.p013 DESC, tbl01.remfdate desc, tbl01.remftime desc, tbl01.nr01 desc 
limit 1;
