Dynamically generate columns for crosstab in PostgreSQL

后端 未结 4 533
慢半拍i
慢半拍i 2020-11-27 05:12

I am trying to create crosstab queries in PostgreSQL such that it automatically generates the crosstab columns instead of hardcoding it. I have wri

4条回答
  •  心在旅途
    2020-11-27 05:41

    You can use the provided C function crosstab_hash for this.

    The manual is not very clear in this respect. It's mentioned at the end of the chapter on crosstab() with two parameters:

    You can create predefined functions to avoid having to write out the result column names and types in each query. See the examples in the previous section. The underlying C function for this form of crosstab is named crosstab_hash.

    For your example:

    CREATE OR REPLACE FUNCTION f_cross_test_db(text, text)
      RETURNS TABLE (kernel_id int, key1 int, key2 int, key3 int)
      AS '$libdir/tablefunc','crosstab_hash' LANGUAGE C STABLE STRICT;
    

    Call:

    SELECT * FROM f_cross_test_db(
          'SELECT kernel_id, key, value FROM test_db ORDER BY 1,2'
         ,'SELECT DISTINCT key FROM test_db ORDER BY 1');
    

    Note that you need to create a distinct crosstab_hash function for every crosstab function with a different return type.

    Related:

    • PostgreSQL row to columns

    Your function to generate the column list is rather convoluted, the result is incorrect (int missing after kernel_id), it can be replaced with this SQL query:

    SELECT 'kernel_id int, '
           || string_agg(DISTINCT key::text, ' int, '  ORDER BY key::text)
           || ' int, DUMMY text'
    FROM   test_db;
    

    And it cannot be used dynamically anyway.

提交回复
热议问题