Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 428CD475DC0 for ; Tue, 17 Dec 2002 08:06:57 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id D3602475AE4 for ; Tue, 17 Dec 2002 08:06:55 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Dec 17 14:06:55 2002 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id OAA19172; Tue, 17 Dec 2002 14:06:55 +0100 (MET) Message-ID: <3DFF216E.32DACAB2@rodos.fzk.de> Date: Tue, 17 Dec 2002 14:06:55 +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/199 X-Sequence-Number: 10787 > As soon as you or 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. Ok, got it: SELECT sid, sname, SUM("OPS") AS "OPS", SUM("MPD") AS "MPD" FROM ( SELECT staff.*, CASE dsdesc WHEN 'OPS' THEN rrank ELSE 0 END AS "OPS", CASE dsdesc WHEN 'MPD' THEN rrank ELSE 0 END AS "MPD" FROM staff,depts,ranks WHERE sid=rsid AND did=rdid ) as foo GROUP BY sid, sname ; sid | sname | OPS | MPD -----+---------+-----+----- 1 | Rod | 0 | 3 2 | Jayne | 5 | 2 3 | Freddie | 3 | 0 (3 rows) Gary, I'm going to write the plpgsql function to dynamically amend the view. In the meantime you may think about creating a trigger which fires every time a new department is entered and which calls the function then. Regards, Christoph