pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: Prasanth A. Kumar <kumar1@home.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Select by priority
Date: Sun, 16 Jul 2000 16:45:54 +0200 (CEST)
Message-ID: <Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain> (raw)
In-Reply-To: <m3ya33co8g.fsf@C654771-a.frmt1.sfba.home.com>

Prasanth A. Kumar writes:

> I need some suggestions on how to construct a particular select that I
> need. I have a table of addresses where the primary key is the
> persons_id and a address_type field. The address_type field is a
> character which specifies whether the address is for the home, work,
> or mailing. A person can have multiple addresses though only one of a
> type. What I want to do is select a list of address for each distinct
> individual but wish to select based of priority that if the mailing
> address exists, I get that one only. If there is no mailing address, I
> want the home address and failing that, get the work address. Is it
> within the realm of sql to be able to do that?

I'd work from this:

SELECT person_id, address, XXX(address_type)
  FROM address_table GROUP BY address, person_id;

where XXX is an aggregate function that selects the highest "priority"
among the address_type codes. If your codes sort alphabetically or
numerically you can just use MIN or MAX, otherwise you'd have to write
your own, which shouldn't be terribly hard.

Instead of person_id you probably want to join against some person table,
but remember that you need to group by every non-aggregate column in the
select list.

-- 
Peter Eisentraut                  Sernanders väg 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden




view thread (6+ messages)  latest in thread

Message-ID: <Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain>
Permalink:  ../Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain/
Also on:    postgresql.org/message-id/Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain

 · 

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: peter_e@gmx.net, kumar1@home.com
  Subject: Re: Select by priority
  In-Reply-To: <Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain>
  References: <m3ya33co8g.fsf@C654771-a.frmt1.sfba.home.com> <Pine.LNX.4.21.0007160454440.379-100000@localhost.localdomain>

* 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