CREATE OR REPLACE FUNCTION get_results() 
RETURNS TABLE ("Пациент" text, "Обследование" text, "Результат" varchar) 
AS $$
BEGIN
    RETURN QUERY(SELECT pa.policy_number, et.name, COALESCE(qr.value::varchar, CONCAT(nr.value || ' ' || u.name))
				FROM patient pa
				LEFT JOIN prescription pr 
					ON pa.patient_id = pr.patient_id
				LEFT JOIN examination_type_prescription etp 
					ON pr.prescription_id = etp.prescription_id
				LEFT JOIN examination_type et
					ON et.examination_type_id = etp.examination_type_id
				LEFT JOIN result r
					ON etp.result_id = r.result_id
				LEFT JOIN qualitative_result qr
					ON r.result_id = qr.result_id
				LEFT JOIN numeric_result nr
					ON r.result_id = nr.result_id
				LEFT JOIN unit u
					ON nr.unit_id = u.unit_id
				WHERE et.name IS NOT NULL);
END;
$$ LANGUAGE plpgsql;