Re: Help with a select statement design

From: Franz Timmer <ml(at)ft-c(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with a select statement design
Date: 2012-12-28 16:12:05
Message-ID: 50DDC4D5.8010500@ft-c.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

hello,

( select 'A' as x, id, price from tab where id = 'value'
union all select 'B' as x, id, ...
union all select 'C' as x, id, ...
union all select 'D' as x, id, price from tab where id = 'value' )

generate a list like
X id price
A 1 10
B 2 20
C 3 30
D 4 40

select
max (case when X = 'A' then id end ) as id_a,
max (case when X = 'A' then price end ) as price_a,
max (case when X = 'B' then id end ) as id_b,
max (case when X = 'B' then price end ) as price_b,
max (case when X = 'C' then id end ) as id_c,
max (case when X = 'C' then price end ) as price_c,
max (case when X = 'D' then id end ) as id_d,
max (case when X = 'D' then price end ) as price_d
from ( table_or_select_from_above )

--- not testet

On 24.12.2012 17:30, JORGE MALDONADO wrote:
> I have a record with the following fields:
>
> id1, id2, id3, id4, price1, price2, price3, price4
>
> I would like to write a SELECT statement in order to get 4 records:
>
> id, price (record that comes from id1 and price 1)
> id, price (record that comes from id2 and price 2)
> id, price (record that comes from id3 and price 3)
> id, price (record that comes from id4 and price 4)
>
> I will very much appreciate any suggestion.
>
> Respectfully,
> Jorge Maldonado

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message johnmethew 2012-12-28 23:11:13 Re: Find Out a Way to Recover Data From Android Phone
Previous Message Andreas Kretschmer 2012-12-28 15:42:59 Re: Help with a select statement design