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


LEFT JOIN LATERAL (
    SELECT
        l.id,
        l.code
    FROM ourpension.contract_product_history cph
    JOIN prod_line.pens_product_hb_print_form pf
        ON pf.pens_product_id = cph.pens_product_id
    JOIN ourpension.landscape_form l
        ON l.type_group_form_id = pf.type_group_form_id
    WHERE cph.contract_id = c.id
      AND CURRENT_TIMESTAMP BETWEEN
          cph.begin_date
          AND COALESCE(cph.end_date, 'infinity')
      AND (l.start_date IS NULL OR l.start_date <= CURRENT_DATE)
      AND (l.end_date IS NULL OR l.end_date >= CURRENT_DATE)
    ORDER BY
        cph.begin_date DESC NULLS LAST,
        l.start_date DESC NULLS LAST,
        l.id DESC
    LIMIT 1
) lf ON TRUE