plpgsql将列名和列统计信息作为表

时间:2016-02-18 13:21:32

标签: plpgsql postgresql-9.3

我试图在postgres数据库中使用pgplpsql来: - 遍历模式中的所有列,对于双精度的每一列,我想返回一个包含字段的表名,列名,最小值,最大值,平均值,中值的表。

- 到目前为止,我已经设法返回这些字段的最小值,最大值,平均值 - 但是尽管在'return语句'中定义了表,但不是在表中。

问题: 如何正确返回表格 如何在此表中包含列的列名和表名?我尝试过很多错误消息。

贝基

DROP FUNCTION household.numeric_stats(schemanm text);
CREATE OR REPLACE FUNCTION household.numeric_stats(schemanm text)
returns table(min double precision, max double precision, avg double precision)as $$

DECLARE
  cname text;
  tname text;

BEGIN
   for cname,tname in SELECT column_name::text col,table_name::text tble FROM information_schema.columns
   where table_schema = schemanm and data_type in ('double precision')
   and table_name::text not in ('ap_household','derived_forest_income', 'derived_product_income','derivedproduct_income','view_income_overview_by_household')

   LOOP

     RAISE NOTICE 'cname is: % from %', cname, tname;
     return query
     execute format('select min(%I), max(%I), avg(%I) from  %I.%I where %I != ''NaN''', cname, cname, cname, schemanm, tname, cname);

END;
$$
LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:0)

会像这样工作:

CREATE OR REPLACE FUNCTION household.numeric_stats(schemanm text)
  RETURNS TABLE(tname text, cname text
              , min float8, max float8
              , avg float8, median float8) AS
$func$
BEGIN
   FOR tname, cname IN
      SELECT table_name::text, column_name::text
      FROM   information_schema.columns
      WHERE  table_schema = schemanm
      AND    data_type = 'double precision'
      AND    table_name <> ALL ('{ap_household,derived_forest_income, derived_product_income,derivedproduct_income,view_income_overview_by_household}'::varchar[])
   LOOP
      -- RAISE NOTICE 'tname: %, cname: %', tname, cname;
      RETURN QUERY EXECUTE format(
      $f$SELECT $1, $2, min(%1$I), max(%1$I), avg(%1$I), median(%1$I)
         FROM   %2$I.%3$I
         WHERE  %1$I <> 'NaN'$f$, cname, schemanm, tname)
      USING tname, cname;
   END LOOP;
END
$func$ LANGUAGE plpgsql;

呼叫:

SELECT * FROM household.numeric_stats('myschema');

您需要先创建中间聚合函数,然后才能使用它。 Postgres Wiki中的说明:
https://wiki.postgresql.org/wiki/Aggregate_Median