select k.__id::text as __id k.__index k.__name k.id_klienta as wz k."__createdAt" as __createdAt k."__updatedAt" as __updatedAt k."__deletedAt" as __deletedAt (select count(*) from elma.deals d where d._companies=k.__id) as deal_cnt (select count(*) from elma.aggrements d where d.company_uid=k.__id) as contract_cnt (select count(*) from elma.additional_agreements g join elma.aggrements d on (g.contract_uid=d.__id) where d.company_uid=k.__id) as aa_cnt from elma.companies k where 1=1 --AND exists (select * from elma.deals d where d._companies=k.__id) order by select count(*) as cnt from elma.companies where 1=1 Контрагенты #__name# #gridFooter#