select
w.warranty_period,
hp.id as invest_profile_id,
hp.name as invest_profile_name
from prod_line.pens_product_param_pl p_w
join prod_line.param_pl def_w
on def_w.id = p_w.param_pl_id
join prod_line.table_param_pl_warrant_period w
on w.pens_product_param_pl_id = p_w.id
join prod_line.table_param_pl_invest_profile ip
on ip.hb_invest_profile_id = w.hb_invest_profileid
join prod_line.pens_product_param_pl p_i
on p_i.id = ip.pens_product_param_pl_id
and p_i.pens_product_id = p_w.pens_product_id
join prod_line.param_pl def_i
on def_i.id = p_i.param_pl_id
join prod_line.hb_invest_profile hp
on hp.id = ip.hb_invest_profile_id
where p_w.pens_product_id = 8977
and def_w.brief = 'ПериодГарантВосполнения'
and def_i.brief = 'ИнвестПрофиль'
and current_date >= coalesce(w.validity_period_from, '-infinity'::date)
and current_date <= coalesce(w.validity_period_to, 'infinity'::date)
and current_date >= coalesce(ip.validity_period_from, '-infinity'::date)
and current_date <= coalesce(ip.validity_period_to, 'infinity'::date);