pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Burton <joel@joelburton.com>
To: Ludwig Lim <lud_nowhere_man@yahoo.com>
Cc: Joseph Syjuco <joseph@asti.dost.gov.ph>
Cc: Postgresql Mailing List <pgsql-sql@postgresql.org>
Subject: Re: simple select statement inquiry
Date: Thu, 6 Jun 2002 12:49:25 -0400 (EDT)
Message-ID: <Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.com> (raw)
In-Reply-To: <20020606033341.29202.qmail@web20409.mail.yahoo.com>

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




view thread (3+ messages)

Message-ID: <Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.com>
Permalink:  ../Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: joel@joelburton.com, lud_nowhere_man@yahoo.com, joseph@asti.dost.gov.ph
  Subject: Re: simple select statement inquiry
  In-Reply-To: <Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.com>
  References: <20020606033341.29202.qmail@web20409.mail.yahoo.com> <Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.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