Re: SQL query help - online music shop - labels & styles - Mailing list pgsql-novice

From Manuel Sugawara
Subject Re: SQL query help - online music shop - labels & styles
Date
Msg-id m3bsoqvd93.fsf@dep1.fciencias.unam.mx
Whole thread Raw
In response to Re: SQL query help - online music shop - labels & styles  (Nabil Sayegh <nsmail@sayegh.de>)
List pgsql-novice
> > BUT! How can I achieve the following??
> > --------------------------------
> >   label                        | style1   | style2   | style3
> > -------------------------------
> >  Matsuri Productions | House  | Techno | Trance
>
>
> Several times I asked if there is a SQL-way to transpone.
> As I didn't get an answer I believe the answer is no.

[...]

> Perhaps it is possible to write a plpgsql-function for it,
> but propably it's not worth the effort.
>

I faced the same problem a few days ago; my case was pretty easy to
solve: first create a function that join the arguments with, say, a
blank space:

  create function concat(text,text) returns text as 'select (case when
  $1 <> '''' then $1 || '' '' else $1 end) || $2' language 'sql';

create an aggregate function. Something like:

  create aggregate concat_agg (
    basetype = text,
    sfunc = concat,
    stype = text,
    initcond = ''
  );

create a temporal test table and insert some values:

  create table tmp(name text, token text);
  insert into tmp values ('masm','lola');
  insert into tmp values ('masm','dola');
  insert into tmp values ('masm','mola');
  insert into tmp values ('masm','pola');
  insert into tmp values ('jsf','kola');
  insert into tmp values ('jsf','dona');
  insert into tmp values ('jsf','poca');

and then select and group by according to your needs:

regression=# select name,concat_agg(token) from tmp group by name;
 name |     concat_agg
------+---------------------
 jsf  | kola dona poca
 masm | lola dola mola pola
(2 rows)
regression=#

HTH,
Manuel.

pgsql-novice by date:

Previous
From: Nabil Sayegh
Date:
Subject: Re: Problems with getting the postmaster to startup with -i option.
Next
From: "Matus \"fantomas\" Uhlar"
Date:
Subject: asynchronous queries