select 
c.__name as contract, d.__name as deal, d.__status_status
,siv.hash ,siv.type, siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual
,p.type as type_p, p.code, p.name, p.price, p.cnt, p.discount as discount_p, p.ed_izm, p.deal as deal_p 
--siv.*
from elma.deals_services siv
join elma.service_parametrs p on (p.__id = ANY (siv.params))
join elma.deals d on (d.__id = ANY(siv.deal))
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.hash='20241011T091519330'
AND siv.is_actual -- убираем черновики расчетов
-- 20241101T110547177 пример странного оформления, лишние типоразмеры в параметрах
-- 20241011T092847849 странным образом меняется строка, цена остается
-- 20241011T091519330 - меняется даже тип 1-2 договор 2206202301
/*
select 
siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual, siv.hash
,p.type, p.code, p.name, p.price, p.cnt, p.discount as discount_p, p.ed_izm, p.deal as deal_p 
--siv.*
from elma.deals_services siv
join elma.service_parametrs p on (p.__id = ANY (siv.params))
where p.cnt <> siv.cnt
*/

select hash from elma.deals_services
where is_actual
group by hash
having count(distinct type) >1

select 
c.__name as contract, d.__name as deal, d.__status_status
,a.__name as additional_agreement
,siv.hash ,siv.type, siv.discount, siv.user_description, siv.cnt, siv.date_nop, siv.date_end, siv.is_easy, siv.deal, siv.is_actual
from elma.deals_services siv
join elma.deals d on (d.__id = ANY(siv.deal))
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.is_actual -- убираем черновики расчетов
AND a.is_actual -- оставляем только актуальные доп.соглашения
AND siv.hash in (select hash 
	from elma.deals_services siv
	join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
	where siv.is_actual AND a.is_actual
	group by hash
	having count(distinct type) >1
)
order by hash

select * from elma.deals_services siv where hash='20240424T170743034' AND is_actual
select * from elma.deals_services siv where hash='20241011T091519330' AND is_actual

select c.__name as contract, a.__name as additional_agreement, deal, hash, count(*) 
from elma.deals_services siv 
join elma.additional_agreements a on (a.__id = ANY(siv.additional_agreement))
join elma.aggrements c on (a.contract_uid = c.__id)
where siv.is_actual 
--AND a.is_actual 
group by siv.deal, c.__name, a.__name, siv.hash, siv.type having count(*)>1
