Re: function to return rows as columns?
От | Linos |
---|---|
Тема | Re: function to return rows as columns? |
Дата | |
Msg-id | 49A7D3A7.9020507@linos.es обсуждение исходный текст |
Ответ на | Re: function to return rows as columns? ("A. Kretschmer" <andreas.kretschmer@schollglas.com>) |
Ответы |
Re: function to return rows as columns?
|
Список | pgsql-general |
A. Kretschmer escribió: > In response to Linos : >> 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 > > Other solution with plain SQL: > > test=*# select * from linos ; > item | size | stock > ------+------+------- > 123 | XL | 10 > 123 | XXL | 5 > 123 | XS | 3 > (3 rows) > > test=*# select item, sum(case when size='XL' then stock else 0 end) as > "XL", sum(case when size='XXL' then stock else 0 end) as "XXL", sum(case > when size='XS' then stock else 0 end) as "XS" from linos where item=123 > group by item; > item | XL | XXL | XS > ------+----+-----+---- > 123 | 10 | 5 | 3 > (1 row) > > > Andreas I think this approach have a problem (almost with my data), i have a somewhat large number of different sizes, about 200 or so (although i have a presented a limited example i now). Thanks anyway by the alternative way to do it Andreas. Regards, Miguel Angel.
В списке pgsql-general по дате отправления: