хорошо, теперь, когда вы отредактировали вопрос, который я понимаю ... вот как я бы это сделал, используя CTE
WITH att2 AS
(
SELECT soi.value, srv.osp_id, soi.stya_id, eoax.estpt_id, eoax.discount, int_sero.id AS sero_id, 2 as attr_number
FROM srv_obj_attr_intermediate soi
JOIN estpt_objt_attr_xref eoax ON eoax.interest_rate = 1
JOIN attribute_types attl ON attl.id = eoax.attr_id
JOIN object_attribute_type_links oatl ON oatl.attr_id = attl.id
JOIN service_type_attributes sta ON sta.objt_attr_id = oatl.id
JOIN service_objects int_sero ON int_sero.id = soi.sero_id
JOIN services srv ON srv.id = int_sero.srv_id
JOIN order_event_types oet ON oet.code = 'CALC_INTERMEDIATE_ESTP'
WHERE eoax.ordet_id = oet.id AND eoax.objt_attr_id = sta.objt_attr_id
AND soi.stya_id = sta.id AND soi.value = 1
), att1 AS
(
SELECT soa.value, srv.osp_id, soa.stya_id, eoax.estpt_id, eoax.discount, int_sero.id AS sero_id, 1 as attr_number
FROM srv_obj_attributes soa
JOIN estpt_objt_attr_xref eoax ON eoax.interest_rate = 1
JOIN attribute_types attl ON attl.id = eoax.attr_id
JOIN object_attribute_type_links oatl ON oatl.attr_id = attl.id
JOIN service_type_attributes sta ON sta.objt_attr_id = oatl.id
--LEFT JOIN srv_obj_attr_intermediate soi ON soi.stya_id = sta.id
--AND soi.value = 1
JOIN service_objects int_sero ON int_sero.id = soa.sero_id
JOIN services srv ON srv.id = int_sero.srv_id
JOIN order_event_types oet ON oet.code = 'CALC_INITIAL_ESTP'
WHERE eoax.ordet_id = oet.id AND eoax.objt_attr_id = sta.objt_attr_id
AND soa.stya_id = sta.id AND soa.value = 1 --AND soi.value IS NULL
), base AS
(
SELECT estpt_id, sero_id, osp_id,
COALESCE(att1,att2) as source
FROM (
SELECT estpt_id, sero_id, osp_id, 1 AS att1, null as att2
FROM att1
GROUP BY estpt_id, sero_id, osp_id
UNION
SELECT estpt_id, sero_id, osp_id, null AS att1, 2 AS att2
FROM att1
GROUP BY estpt_id, sero_id, osp_id
)
)
SELECT
COALESCE(att1.value,att2.value) as value,
base.osp_id,
COALESCE(att1.stya_id,att2.stya_id) as stya_id,
base.estpt_id,
COALESCE(att1.discount,att2.discount) as discount,
eoax.discount,
base.sero_id
FROM base
LEFT JOIN att1 ON base.estpt_id = att1.estpt_id and base.sero_id = att1.sero_id and base.osp_id and att1.osp_id and source = 1
LEFT JOIN att2 ON base.estpt_id = att2.estpt_id and base.sero_id = att2.sero_id and base.osp_id and att2.osp_id and source = 2