Так вот мой sql select count(m.*)
from (
select hst.esn, hst.owner,
row_number() over (partition by hst.esn, hst.owner) as rown,
p.code
from product_history hst
left join product p on hst.esn = p.esn
where hst.from_date between cast('01.01.2021'as date) and cast('01.12.2021' as date)
and hst.status = 'Activated' and hst.owner is not null
) m where m.rown = 1