Загрузка данных


верный запрос, без дубликатов и нужными договорами

WITH q AS (
    SELECT DISTINCT ON (c.number)
        i.last_name as last_name,
        i.first_name as first_name,
        i.middle_name as middle_name,
        c.number as contract_number,
        to_char(c.date, 'DD.MM.YYYY') as contract_date,
        atc."date" as atc_date,
        atc.id as atc_id
    from ourpension.contract c
left join ourpension.individual i on i.id = c.individual_id
join ourpension.sharer sh on c.id = sh.contract_id
left join ourpension.application_termination_contract atc on atc.sharer_id = sh.id
--left join ourpension.year_tax_deduction td on td.application_termination_contract_id = atc.id
--left join ourpension.service_app_type sat on atc.service_app_type_code = sat.code
--where atc."date" >= to_date(:start_date_app, 'YYYY.MM.DD')
--and atc."date" < to_date(:end_date_app, 'YYYY.MM.DD') + interval '1 day'
where atc."date" >=
    case
        when :start_date_app = 'now()' then current_date
        when :start_date_app = 'now() - interval 24 hours' then current_date - interval '1 day'
        else to_date(:start_date_app, 'YYYY.MM.DD')
    end
and atc."date" <
    case
        when :end_date_app = 'now()' then current_date
        when :end_date_app = 'now() - interval 24 hours' then current_date - interval '1 day'
        else to_date(:end_date_app, 'YYYY.MM.DD')
    end + interval '1 day'
--and ( :isChecked = false or td.type != 'RECEIVED')
and i.first_name is not null
and ( :isChecked = false or exists (select 1 from ourpension.year_tax_deduction td where td.application_termination_contract_id = atc.id and td.type != 'RECEIVED'))

)
SELECT
  q.last_name as last_name,
  q.first_name as first_name,
  q.middle_name as middle_name,
  q.contract_number as contract_number,
  q.contract_date as contract_date 
FROM q

запрос требующий корректировки

select  DISTINCT ON (c.number)
 to_char(i.birth_date, 'DD.MM.YYYY') as birth_date,
  i.inn as person_inn,
  i.last_name,
  i.first_name,
  i.middle_name,
  i.certificate_date,
  i.certificate_number || ' ' || i.certificate_series as ser_nom_doc,
  i.certificate_code,
  i.id as smev_register_id,
	to_char(now(), 'DD.MM.YYYY') as date_doc,
  -- атрибуты Файл
  'UT_ZAPRSTRORGNPF_' || i.id || '_' || to_char(i.birth_date,'YYYYMMDD') || '_' || replace(cast(gen_random_uuid() as text),'-','') as file_id,
  '5.02' as vers_form,
  'КОНТУР-ЭКСТЕРН, ВЕРСИЯ 16.0' as vers_prog,
  -- атрибуты Документ
  '7702' as kod_no,
  to_char(now(), 'DD.MM.YYYY') as doc_date,
  '1184042' as knd,
  -- СвНП/НПЮЛ
  f.kpp as org_kpp,
  f.inn as org_inn,
  f.name_full as org_name,
  (
    select coalesce(max(cast(request_number as int)), 0) + 1 
    from report.fns_reference_request_journal
  ) as req_number,
  c.number as number,
to_char(c.date, 'DD.MM.YYYY') as contract_date,
'4' as pr_c,
'1' as pr_z_cont
from ourpension.application_termination_contract atc
left join ourpension.year_tax_deduction td on td.application_termination_contract_id = atc.id
left join ourpension.individual i on i.id = atc.individual_id
left join ourpension.fund f on i.fund_id = f.id
left join ourpension.sharer s on s.individual_id = atc.individual_id
left join ourpension.contract c on s.contract_id = c.id and (c."service_type" ='NPO' or c."service_type" ='PDS')
left join ourpension.uv_activity_type uat 
			 on uat.activity_eng_type = c.service_type 
			and uat.client_type = c.client_type
where atc."date" >= to_date(:start_date_app, 'YYYY.MM.DD')
and atc."date" < to_date(:end_date_app, 'YYYY.MM.DD') + interval '1 day'
and ( :isChecked = false or td.type != 'RECEIVED')
and i.first_name is not null
and (uat.activity_rus_type = 'ПДС' or uat.activity_rus_type = 'НПО') and atc.id is not null
ORDER BY c.number, i.id