How to derive a column name in the return type from input parameters to the function?
<p>Using Postgres 9.5 I have built this function:</p>

<pre><code>CREATE or REPLACE FUNCTION func_getratio_laglag(_numeratorLAG text, _n1 int, _denominatorLAG text, _n2 int, _table text)
    RETURNS TABLE (date_t timestamp without time zone, customer_code text, index text, ratio real) AS
$BODY$
BEGIN
 RETURN QUERY EXECUTE 
        'SELECT 
    date_t,
    customer_code,
    index,
        (LAG('||quote_ident(_numeratorLAG)||',' || quote_literal(_n1)||') OVER W / LAG('||quote_ident(_denominatorLAG)||','|| quote_literal(_n2)||') OVER W) '
         || ' FROM ' || quote_ident(_table) 
         || ' WINDOW W AS (PARTITION BY customer_code ORDER BY date_t asc);';
END;
$BODY$ LANGUAGE plpgsql;
</code></pre>

<p>All the function does is allow me the ability to pick a 2 different columns from a specified table and calculate a ratio between them based on different lag windows. To execute the function above I use the following query:</p>

<pre><code>SELECT * FROM func_getratio_laglag('order_first',1,'order_last',0,'customers_hist');
</code></pre>

<p>This returns a table with the column labels <code>date_t</code>, <code>customer_code</code>, <code>index</code> and <code>ratio</code>. I have really struggled on how to output ratio as a dynamic column label. That is, I would like to make it contingent on the input parameters e.g. if I ran the select query above then I would like the column labels <code>date_t</code>, <code>customer_code</code>, <code>index</code> and <strong><code>order_first_1_order_last_0</code></strong>.</p>

<p>I am stuck, any advice or hints?</p>