Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 85600475AAC for ; Tue, 17 Dec 2002 07:06:02 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 72AA1475461 for ; Tue, 17 Dec 2002 07:06:01 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Dec 17 13:06:01 2002 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id NAA19117; Tue, 17 Dec 2002 13:06:01 +0100 (MET) Message-ID: <3DFF1328.13B365D6@rodos.fzk.de> Date: Tue, 17 Dec 2002 13:06:01 +0100 From: Christoph Haller X-Mailer: Mozilla 4.06 [en] (X11; I; HP-UX B.10.20 9000/780) MIME-Version: 1.0 To: gary.stainburn@ringways.co.uk Cc: pgsql-sql@postgresql.org Subject: Re: join and dynamic view Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/196 X-Sequence-Number: 10784 > > is it possible to make a dynamically declare a view based on a table? > Yes, by all means. > > Is it possible to now define a view such that it returns: > > select * from myview; > sid | Name | OPS | MPD > -----+---------+-----+----- > 1 | Rod | | 3 > 2 | Jayne | 2 | 5 > 3 | Freddie | 3 | > > and if I add another row to depts, that the new row would be included? > ^^^^^^^^^^^^^^^^ you mean column, don't you? The closest query I can get so far is SELECT staff.*, CASE dsdesc WHEN 'OPS' THEN rrank ELSE NULL END AS "OPS", CASE dsdesc WHEN 'MPD' THEN rrank ELSE NULL END AS "MPD" FROM staff,depts,ranks WHERE sid=rsid AND did=rdid ; sid | sname | OPS | MPD -----+---------+-----+----- 1 | Rod | | 3 2 | Jayne | | 2 2 | Jayne | 5 | 3 | Freddie | 3 | (4 rows) but sid | sname | OPS | MPD -----+---------+-----+----- 1 | Rod | | 3 2 | Jayne | 5| 2 3 | Freddie | 3 | (3 rows) is what you want (I suppose Jayne's 2 in OPS and 5 in MPD is a mismatch of yours). As soon as you are somebody else can tell me how to merge Jayne's two rows into one, I'm sure I can write a plpgsql function to dynamically create the view you're looking for. Regards, Christoph