you probably can use a correlated subquery to do the ranking in your select.
something like the following but using your tables and columns etc.
select yourcolumns,
(select count(*) from table1 t2
where t2.spid < table1.spid
and t2.vehicleid = table1.vehicleid and t2.tripid = table1.tripid
) as rank
from table1
where ....