Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 46AC04760D1 for ; Thu, 6 Jun 2002 12:12:43 -0400 (EDT) Received: from temp.joelburton.com (unknown [209.190.238.101]) by postgresql.org (Postfix) with ESMTP id E7F3D47657E for ; Thu, 6 Jun 2002 12:12:41 -0400 (EDT) Received: by temp.joelburton.com (Postfix, from userid 501) id 2BDAE2BD09; Thu, 6 Jun 2002 12:49:25 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by temp.joelburton.com (Postfix) with ESMTP id 205FD2BA2B; Thu, 6 Jun 2002 12:49:25 -0400 (EDT) Date: Thu, 6 Jun 2002 12:49:25 -0400 (EDT) From: Joel Burton To: Ludwig Lim Cc: Joseph Syjuco , Postgresql Mailing List Subject: Re: simple select statement inquiry In-Reply-To: <20020606033341.29202.qmail@web20409.mail.yahoo.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200206/56 X-Sequence-Number: 8021 On Wed, 5 Jun 2002, Ludwig Lim wrote: > > desired output > > empno peer superior > > 1000 John Smith Henry Dunst > > 2000 Juan dela Cruz Pepe Smith > > Create a stored function that will return the fullname > of the "peer" or "superior" given an employee number Or, much faster, in a single statement: SELECT e.empno, p.fname || ' ' || p.lname AS peer, s.fname || ' ' || s.lname AS superior FROM emp1 AS e, emp2 AS p, emp2 AS s WHERE e.peerno = p.empno AND e.supno = s.empno This assumes that every person in emp will have non-null values for the peer and superior columns. If someone didn't, they wouldn't appear in this input. To fix this, you could re-write this using LEFT OUTER JOINs from emp1 to the two emp2's. This also assumes that neither fname or lname will be null (if either or both were, the fullname would be null). You can fix this with a COALESCE. HTH. - J. -- Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton Independent Knowledge Management Consultant