select left(k.__name,50) as contragent, k.__id as contragent_uid, c.__name as contract, c.__id as contract_id 
,left(a.__name,50) as additional_agreement, a.deal_uid, a.is_actual, a.last_aa
,left(d.__name,50) as deal, d.__status_status as deal_status, d."_plannedDueDate"
from elma.aggrements c
join elma.additional_agreements a on (c.__id=a.contract_uid)
join elma.companies k on (c.company_uid=k.__id)
join elma.deals d on (a.deal_uid=d.__id)
order by 1;

select 
s.hash
,case when length(s.user_description) >0 then left(s.user_description,50) else s.abstractive_service end as svc
,d."_plannedDueDate"::date, s.date_nop::date, s.date_end::date, s.cnt, s.is_easy 
,p.code, p.cnt as quantity, p.price, p.discount
,left(k.__name,50) as contragent, k.__id as contragent_uid, c.__name as contract, c.__id as contract_uid 
,left(a.__name,50) as additional_agreement, a.deal_uid, left(d.__name,50) as deal, d.__index as deal_number
,a.is_actual, a.last_aa, a.index as agreement_version
,d.__status_status as deal_status 
,c.__name as contract
from elma.aggrements c
join elma.additional_agreements a on (c.__id=a.contract_uid)
join elma.companies k on (c.company_uid=k.__id)
join elma.deals d on (a.deal_uid=d.__id)
join elma.deals_services s on (d.__id=ANY(s.deal))
join elma.service_parametrs p on(p.__id=ANY(s.params))
order by 1
limit 100;
