2

使用 Postgres 9.5 我构建了这个函数:

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;

该函数所做的只是让我能够从指定的表中选择 2 个不同的列,并根据不同的滞后窗口计算它们之间的比率。要执行上面的函数,我使用以下查询:

SELECT * FROM func_getratio_laglag('order_first',1,'order_last',0,'customers_hist');

这将返回一个带有列标签date_tcustomer_codeindex的表ratio。我一直在努力解决如何将比率输出为动态列标签。也就是说,我想让它取决于输入参数,例如,如果我运行上面的选择查询,那么我想要列标签date_t、、customer_code和。indexorder_first_1_order_last_0

我被卡住了,有什么建议或提示吗?

4

1 回答 1

2

如何从函数的输入参数导出返回类型中的列名?

简短的回答:不可能。
SQL 对列数据类型和名称非常严格。这些必须在最迟在通话时间之前或通话时声明。没有例外。没有真正动态的列名。

我可以想到 3 个中途解决方法:

1.列别名

按原样使用您的函数(或者更确切地说是我在下面建议的审核版本)并在函数调用中添加列别名:

SELECT * FROM func_getratio_laglag('order_first',1,'order_last',0,'customers_hist')
AS f(date_t, customer_code, index, order_first_1_order_last_0)

我会那样做。

2.列定义列表

创建您的函数以返回匿名记录:

RETURNS SETOF record

然后,您必须在每次调用时提供一个列定义列表:

SELECT * FROM func_getratio_laglag('order_first',1,'order_last',0,'customers_hist')
AS f(date_t timestamp, customer_code text, index text, order_first_1_order_last_0 real)

我不会那样做。

3. 使用注册的行类型作为多态输入/输出类型。

如果您碰巧手头有行类型,这将非常有用。您可以通过创建一个临时表来动态注册一个行类型,但这对于您的用例来说似乎有点过分了。

此答案最后一章中的详细信息:

功能审核

用于format()使构建查询字符串更加安全和简单。
如果您不熟悉该手册,请阅读该手册

CREATE OR REPLACE FUNCTION func_getratio_laglag(
                           _numerator_lag   text, _n1 int
                         , _denominator_lag text, _n2 int
                         , _table regclass)
   RETURNS TABLE (date_t timestamp, customer_code text, index text, ratio real) AS
$func$
BEGIN
   RETURN QUERY EXECUTE format (
     'SELECT date_t, customer_code, index
           , (lag(%I, %s) OVER w / lag(%I, %s) OVER w)  -- data type must match
      FROM   %s 
      WINDOW w AS (PARTITION BY customer_code ORDER BY date_t)'
    , _numerator_lag, _n1, _denominator_lag, _n2, _table::text
   );
END
$func$ LANGUAGE plpgsql;

请注意表名的数据类型regclass。这是我个人(可选)的建议。

另外:我还建议不要在 Postgres 中使用大小写混合的标识符。

于 2016-03-20T04:05:40.173 回答