pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with a select statement design
3+ messages / 3 participants
[nested] [flat]

* Help with a select statement design
@ 2012-12-24 16:30  JORGE MALDONADO <jorgemal1960@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: JORGE MALDONADO @ 2012-12-24 16:30 UTC (permalink / raw)
  To: pgsql-sql

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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with a select statement design
@ 2012-12-28 15:42  Andreas Kretschmer <andreas@a-kretschmer.de>
  parent: JORGE MALDONADO <jorgemal1960@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Andreas Kretschmer @ 2012-12-28 15:42 UTC (permalink / raw)
  To: pgsql-sql@postgresql.org, JORGE MALDONADO <jorgemal1960@gmail.com>



JORGE MALDONADO <jorgemal1960@gmail.com> hat am 24. Dezember 2012 um 17:30
geschrieben:
> 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

select id1 as id, price1 as price
union all
select id2, price2
union all
select id3, ... you got it?


Andreas


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with a select statement design
@ 2012-12-28 16:12  Franz Timmer <ml@ft-c.de>
  parent: JORGE MALDONADO <jorgemal1960@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Franz Timmer @ 2012-12-28 16:12 UTC (permalink / raw)
  To: 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


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2012-12-28 16:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-12-24 16:30 Help with a select statement design JORGE MALDONADO <jorgemal1960@gmail.com>
2012-12-28 15:42 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2012-12-28 16:12 ` Franz Timmer <ml@ft-c.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox