select count(*) from flow_inst A,step_inst B,step_inst B2,deal_info C,base_user D,tree_def T where A.flow_inid=C.flow_inid and B.step_inco=C.step_inco
and C.peri_man=D.user_code(+) and A.curr_step=B2.step_inco and T.item_seq=A.flow_id
and (C.step_inco ||' '|| to_char(C.deal_indx)) in
( select max(CC.step_inco ||' '|| to_char(CC.deal_indx) ) from flow_inst AA,deal_info CC,base_user DD,base_user DD2,tree_def TT
where AA.flow_inid=CC.flow_inid and CC.peri_man=DD.user_code(+) and AA.inst_crea=DD2.user_code(+) and AA.flow_id=TT.item_seq
and (AA.del_flag is null or AA.del_flag=0) and CC.fini_flag is not null and CC.step_type<>5 and (1=1) group by AA.flow_inid )
order by B.crea_date desc