Thread: crosstab function - pivot - use column value as column description

crosstab function - pivot - use column value as column description

From
Daniel Schuchardt
Date:
Hy,

i'm looking for a solution to get this table dynamicaly in that resultset:

SELECT r_dbrid, r_kategorie AS column_name, r_descr AS value FROM
recnokeyword WHERE r_dbrid=X
  r_dbrid  | r_kategorie |  r_descr
----------+-------------+------------
  14725737 |material | Alu Sorte1
  14725737 |farbe | Blau
  14725737 |gewicht | 100 kg

---->

material | farbe | gewicht
----------+-------------+------------
alu sorte 1|blau | 100kg

----> the number of columns depends on data.

is there is a simple solution for that?
--

Daniel Schuchardt
/Softwareentwicklung/

/http://www.prodat-sql.de/


Re: crosstab function - pivot - use column value as column description

From
Merlin Moncure
Date:
On Tue, Jan 12, 2010 at 3:27 PM, Daniel Schuchardt
<d.schuchardt@prodat-sql.de> wrote:
> Hy,
>
> i'm looking for a solution to get this table dynamicaly in that resultset:
>
> SELECT r_dbrid, r_kategorie AS column_name, r_descr AS value FROM
> recnokeyword WHERE r_dbrid=X
>  r_dbrid  | r_kategorie |  r_descr
> ----------+-------------+------------
>  14725737 |material | Alu Sorte1
>  14725737 |farbe | Blau
>  14725737 |gewicht | 100 kg
>
> ---->
>
> material | farbe | gewicht
> ----------+-------------+------------
> alu sorte 1|blau | 100kg
>
> ----> the number of columns depends on data.
>
> is there is a simple solution for that?

have you ruled out the appropriate contrib module?

http://www.postgresql.org/docs/8.4/static/tablefunc.html

merlin

Re: crosstab function - pivot - use column value as column description

From
Daniel Schuchardt
Date:
See bottom.

Am 12.01.2010 22:00, schrieb Merlin Moncure:
> On Tue, Jan 12, 2010 at 3:27 PM, Daniel Schuchardt
> <d.schuchardt@prodat-sql.de>  wrote:
>
>> Hy,
>>
>> i'm looking for a solution to get this table dynamicaly in that resultset:
>>
>> SELECT r_dbrid, r_kategorie AS column_name, r_descr AS value FROM
>> recnokeyword WHERE r_dbrid=X
>>   r_dbrid  | r_kategorie |  r_descr
>> ----------+-------------+------------
>>   14725737 |material | Alu Sorte1
>>   14725737 |farbe | Blau
>>   14725737 |gewicht | 100 kg
>>
>> ---->
>>
>> material | farbe | gewicht
>> ----------+-------------+------------
>> alu sorte 1|blau | 100kg
>>
>> ---->  the number of columns depends on data.
>>
>> is there is a simple solution for that?
>>
> have you ruled out the appropriate contrib module?
>
> http://www.postgresql.org/docs/8.4/static/tablefunc.html
>
> merlin
>
>

yes, but that function requires a return type -> so i has to know how
many columns. also the column description ist defined by data but by
return type.

(

SELECT * FROM crosstab('...', '...')
       AS ct(row_name text, extra text, cat1 text, cat2 text, cat3 text, cat4 text);


This will produce a result something like:

                               <==  value  columns   ==>
            row_name   extra   cat1   cat2   cat3   cat4

)

Daniel
--

Daniel Schuchardt
/Softwareentwicklung/

/http://www.prodat-sql.de/


Re: crosstab function - pivot - use column value as column description

From
Pavel Stehule
Date:
Hello

2010/1/12 Daniel Schuchardt <d.schuchardt@prodat-sql.de>:
> Hy,
>
> i'm looking for a solution to get this table dynamicaly in that resultset:
>
> SELECT r_dbrid, r_kategorie AS column_name, r_descr AS value FROM
> recnokeyword WHERE r_dbrid=X
>  r_dbrid  | r_kategorie |  r_descr
> ----------+-------------+------------
>  14725737 |material | Alu Sorte1
>  14725737 |farbe | Blau
>  14725737 |gewicht | 100 kg
>
> ---->
>
> material | farbe | gewicht
> ----------+-------------+------------
> alu sorte 1|blau | 100kg
>
> ----> the number of columns depends on data.
>
> is there is a simple solution for that?
> --

look on http://okbob.blogspot.com/2008_08_01_archive.html

Regards
Pavel Stehule

>
> Daniel Schuchardt
> /Softwareentwicklung/
>
> /http://www.prodat-sql.de/
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

Re: crosstab function - pivot - use column value as column description

From
Pavel Stehule
Date:
pardon,

correct link http://okbob.blogspot.com/2008/08/using-cursors-for-generating-cross.html

pavel

2010/1/13 Pavel Stehule <pavel.stehule@gmail.com>:
> Hello
>
> 2010/1/12 Daniel Schuchardt <d.schuchardt@prodat-sql.de>:
>> Hy,
>>
>> i'm looking for a solution to get this table dynamicaly in that resultset:
>>
>> SELECT r_dbrid, r_kategorie AS column_name, r_descr AS value FROM
>> recnokeyword WHERE r_dbrid=X
>>  r_dbrid  | r_kategorie |  r_descr
>> ----------+-------------+------------
>>  14725737 |material | Alu Sorte1
>>  14725737 |farbe | Blau
>>  14725737 |gewicht | 100 kg
>>
>> ---->
>>
>> material | farbe | gewicht
>> ----------+-------------+------------
>> alu sorte 1|blau | 100kg
>>
>> ----> the number of columns depends on data.
>>
>> is there is a simple solution for that?
>> --
>
> look on http://okbob.blogspot.com/2008_08_01_archive.html
>
> Regards
> Pavel Stehule
>
>>
>> Daniel Schuchardt
>> /Softwareentwicklung/
>>
>> /http://www.prodat-sql.de/
>>
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general
>>
>