select *
from clients c
where exists (
select null
from (select abs(principal - lag(principal) over(partition by client_id order by report_date)) diff
from principals p
where p.client_id = c.client_id
)
where diff < c.payment_need
and diff is not null)
with function f (p_login varchar2) return varchar2
is
l_retval varchar2(32000);
begin
for x in (select id from t where login = p_login)
loop
l_retval:= l_retval||'|'||x.id;
end loop;
return l_retval;
end;
select plogin, f(plogin) from (
select distinct login as plogin from t
)
/