RE: Select by priority

From: "Gary J(dot) Farmer" <farmer(at)arlut(dot)utexas(dot)edu>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: RE: Select by priority
Date: 2000-07-17 22:12:54
Message-ID: NDBBKNPOIKAFAGAOPAPNCEMCCBAA.farmer@arlut.utexas.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>> I think the order thing will work though I have no choice of using
>> numbering for the address_type as I am working off a pre-existing
>> database. They are using a mnemonic char type. I am essentially batch
>> downloading and processing this and other database tables for query
>> and presentations using web pages. BTW, does the 'limit' feature exist
>> in Oracle? The main database I am extracting data from is Oracle...

I do not know whether you can add a table associating "priority" with
"address_type". If you can, you might try something like the following
example, using the address_priority table:

create table address_table (
person_id integer,
address_type varchar(1),
address varchar(50)
);

insert into address_table values ( 1, 'W', 'ROUTE 1, WORK ST');

insert into address_table values ( 2, 'H', 'ROUTE 2, HOME AVE');

insert into address_table values ( 3, 'W', 'ROUTE 3, WORK ST');
insert into address_table values ( 3, 'H', 'ROUTE 3, HOME AVE');

insert into address_table values ( 4, 'M', 'ROUTE 4, MAIL RD');

insert into address_table values ( 5, 'M', 'ROUTE 5, MAIL RD');
insert into address_table values ( 5, 'W', 'ROUTE 5, WORK ST');

insert into address_table values ( 6, 'M', 'ROUTE 6, MAIL RD');
insert into address_table values ( 6, 'H', 'ROUTE 6, HOME AVE');

insert into address_table values ( 7, 'M', 'ROUTE 7, MAIL RD');
insert into address_table values ( 7, 'H', 'ROUTE 7, HOME AVE');
insert into address_table values ( 7, 'W', 'ROUTE 7, WORK ST');

create table address_priority (
address_type varchar(1),
priority integer
);

insert into address_priority values ( 'M', 1 );
insert into address_priority values ( 'H', 2 );
insert into address_priority values ( 'W', 3 );

select person_id, address
from address_table a, address_priority b
where
(person_id, priority) in
(select person_id, min(priority)
from address_table a, address_priority b
where a.address_type = b.address_type
group by person_id) and
a.address_type = b.address_type;

PERSON_ID ADDRESS
---------- --------------------------------------------------
1 ROUTE 1, WORK ST
2 ROUTE 2, HOME AVE
3 ROUTE 3, HOME AVE
4 ROUTE 4, MAIL RD
5 ROUTE 5, MAIL RD
6 ROUTE 6, MAIL RD
7 ROUTE 7, MAIL RD

Appears to work with either Oracle or Postgres (though I changed VARCHAR
to VARCHAR2 for Oracle).

Gary Farmer

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Carolyn Lu Wong 2000-07-18 02:01:46 Database authentication and configuration
Previous Message Tom Lane 2000-07-17 07:03:41 Re: SQL question