Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:11:24+00:00 2026-06-12T21:11:24+00:00

I am trying to create crosstab queries in PostgreSQL such that it automatically generates

  • 0

I am trying to create crosstab queries in PostgreSQL such that it automatically generates the crosstab columns instead of hardcoding it. I have written a function that dynamically generates the column list that I need for my crosstab query. The idea is to substitute the result of this function in the crosstab query using dynamic sql.

How do I do it in PostgreSQL?

-- Table which has be pivoted
CREATE TABLE test_db
(
    kernel_id int,
    key int,
    value int
);

INSERT INTO test_db VALUES
(1,1,99),
(1,2,78),
(2,1,66),
(3,1,44),
(3,2,55),
(3,3,89);


-- This function dynamically returns the list of columns for crosstab
CREATE FUNCTION test() RETURNS TEXT AS '
DECLARE
    key_id int;
    text_op TEXT = '' kernel_id int, '';
BEGIN
    FOR key_id IN SELECT DISTINCT key FROM test_db ORDER BY key LOOP
    text_op := text_op || key_id || '' int , '' ;
    END LOOP;
    text_op := text_op || '' DUMMY text'';
    RETURN text_op;
END;
' LANGUAGE 'plpgsql';

-- This query works. I just need to convert the static list
-- of crosstab columns to be generated dynamically.
SELECT * FROM
crosstab
(
    'SELECT kernel_id, key, value FROM test_db ORDER BY 1,2',
    'SELECT DISTINCT key FROM test_db ORDER BY 1'
)
AS x (kernel_id int, key1 int, key2 int, key3 int); -- How can I replace ..
-- .. this static list with a dynamically generated list of columns ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:11:25+00:00Added an answer on June 12, 2026 at 9:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create this function such that if any key besides any of the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I am trying create a small web application that allows a user to login
Trying to create a simple document that gets the parentNode then applies a background
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying create a query that will output a total number, as well as
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.