Re: SQL query help - online music shop - labels & styles
От | Manuel Sugawara |
---|---|
Тема | Re: SQL query help - online music shop - labels & styles |
Дата | |
Msg-id | m3bsoqvd93.fsf@dep1.fciencias.unam.mx обсуждение исходный текст |
Ответ на | Re: SQL query help - online music shop - labels & styles (Nabil Sayegh <nsmail@sayegh.de>) |
Список | 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 по дате отправления: