Re: simple select statement inquiry

From: Joel Burton <joel(at)joelburton(dot)com>
To: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>
Cc: Joseph Syjuco <joseph(at)asti(dot)dost(dot)gov(dot)ph>, Postgresql Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: simple select statement inquiry
Date: 2002-06-06 16:49:25
Message-ID: Pine.LNX.4.30.0206061245120.12975-100000@temp.joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
Independent Knowledge Management Consultant

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2002-06-06 17:01:37 Re: Indexing timestamps
Previous Message Roberto Mello 2002-06-06 16:10:06 Re: extract and variables in PL/pgSQL