pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: miker@n2bb.com
Cc: pgsql-sql@postgresql.org
Subject: Re: casting to arrays
Date: Fri, 18 Jul 2003 14:34:43 -0700
Message-ID: <3F1867F3.7000109@joeconway.com> (raw)
In-Reply-To: <200307181707.48441.miker@n2bb.com>
References: <200307181707.48441.miker@n2bb.com>

Mike Rylander wrote:
> I have a rather odd table structure that I would like to simplify to be a view 
> (for some definition of simplify).  The current idea I have is to shovel 
> values from multiple rows in one table into an array in the view.  The tables 
> look something like this:
> 
<snip>
> 
> Is anything like this possible?  I know this may not be good form, but 
> unfortunately (or perhaps fortunately, since it means I have a job) there are 
> business reasons for this, supporting old apps and such.
> 

Not possible in current releases, but it will be in 7.4 (about to start 
beta). It looks like this:

create table person (id  integer, name  varchar);
insert into person values(1,'Bob');
insert into person values(2,'Sue');

create table stuff (person_id integer, stuff_name text);
insert into stuff values(1,'chair');
insert into stuff values(1,'couch');
insert into stuff values(1,'lamp');
insert into stuff values(2,'table');
insert into stuff values(2,'shirt');

create or replace view person_with_stuff as select p.id as id, p.name as 
name, ARRAY(select s.stuff_name from stuff s where s.person_id = p.id) 
as stuff from person p;

regression=# select * from person_with_stuff;
  id | name |       stuff
----+------+--------------------
   1 | Bob  | {chair,couch,lamp}
   2 | Sue  | {table,shirt}
(2 rows)

HTH,

Joe




view thread (5+ messages)  latest in thread

Message-ID: <3F1867F3.7000109@joeconway.com>
Permalink:  ../3F1867F3.7000109@joeconway.com/
Also on:    postgresql.org/message-id/3F1867F3.7000109@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, miker@n2bb.com
  Subject: Re: casting to arrays
  In-Reply-To: <3F1867F3.7000109@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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