You still need a subquery, but analytic functions are a bit more elegant:
CREATE VIEW COMPANY_LAST_ACQ_CONTACT as
select id, company_id, be_user_id, created_datetime, text
from (select ci.id, ci.company_id, ci.be_user_id, ci.created_datetime, ci.text,
max(id) over (partition by company_id) as maxid
from crm_contact
) c
where id = maxid;
Another efficient way to write the query -- if you have in index on company_id, id
is to use not exists
:
select ci.id, ci.company_id, ci.be_user_id, ci.created_datetime, ci.text
from crm_contact c
where not exists (select 1` from crm_contact cc on c.company_id = cc.company_id and cc.id > id);