Re: function to return rows as columns?

From: Linos <info(at)linos(dot)es>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: function to return rows as columns?
Date: 2009-02-27 11:16:15
Message-ID: 49A7CB7F.2050606@linos.es
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thomas Kellerer escribió:
> Linos, 27.02.2009 11:41:
>> Hello,
>> i have a query that returns a result set like this:
>>
>> item | size | stock
>> 123 | XL | 10
>> 123 | XXL | 5
>> 123 | XS | 3
>>
>> and i would like get the results like this:
>>
>> item | XL | XXL | XS
>> 123 | 10 | 5 | 3
>>
>> i have been thinking how to do it with a plpgsql function but the
>> number of sizes depend on the item that it is queried so i can not
>> create a type and return it, i could create it like a text
>> concatenating the stock and size of every row and returning the
>> complete line text but i would have to process it in the application
>> anyway so i am searching a solution that lets me return it like a record.
>>
>> I have been searching the list and maybe i could create the record
>> type inside the function and to get the correct names and number of
>> columns in the application side launching a query to get the number of
>> sizes before call the function to specify the columns in the function
>> call but maybe i am missing anything important here? any better (or
>> more correct) way to do this? Thanks.
>
> Check out the "crosstab" function in the "Tablefunc" module:
>
> http://www.postgresql.org/docs/current/static/tablefunc.html
>
>

I knew i was missing something hehehe, thanks Thomas.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2009-02-27 11:37:39 Re: function to return rows as columns?
Previous Message Zeugswetter Andreas OSB sIT 2009-02-27 11:15:57 RE: [HACKERS] RE: [HACKERS] Kerberos V5 required for PostgreSQL installation on Windows