with ps as (select ps.* from personstate ps
--join evnpl el on el.person_id = ps.person_id
--join evnps es on es.person_id = ps.person_id
where
(
exists(select 1 from  evnpl where person_id = ps.person_id)
or
exists(select 1 from  evnps where person_id = ps.person_id)
)
and not exists (select 1 from personcard where person_id = ps.person_id)
--and ps.person_id = 120101000006392
)
,
result as (select --distinct
concat(ps.personsurname_surname,' ',ps.personfirname_firname,' ',ps.personsecname_secname) as "ФИО", 
to_char(ps.personbirthday_birthday, 'dd.MM.yyyy') as "дата рождения", ps.personpolisednum_ednum as "ЕНП",
o.org_name as "МО лечения", 
ua.address_address as "адрес регистрации", pa.address_address as "адрес проживания"
from ps
join evnpl el on el.person_id = ps.person_id 
join lpu l on l.lpu_id = el.lpu_id
join org o on o.org_id = l.org_id
left join address ua on ua.address_id = ps.uaddress_id
left join address pa on ua.address_id = ps.paddress_id

union all 

select --distinct
concat(ps.personsurname_surname,' ',ps.personfirname_firname,' ',ps.personsecname_secname) as "ФИО", 
to_char(ps.personbirthday_birthday, 'dd.MM.yyyy') as "дата рождения", ps.personpolisednum_ednum as "ЕНП",
o.org_name as "МО лечения", 
ua.address_address as "адрес регистрации", pa.address_address as "адрес проживания"
from ps
join evnps es on es.person_id = ps.person_id 
join lpu l on l.lpu_id = es.lpu_id
join org o on o.org_id = l.org_id
left join address ua on ua.address_id = ps.uaddress_id
left join address pa on ua.address_id = ps.paddress_id)

select distinct * from result
order by "ФИО" asc
