Reputation: 19
I am working on a query that draws data from several tables under certain conditions. The columns in the result set have to include values for 1-3 types of addresses. This is a typical 1 Patron to 1,3 Patron Address relation.
There are three address blocks needed in my result set. (This is for a system conversion from on ILS to another in our academic library.)
Everyone, from what I can tell, has a row in the PATRON_ADDRESS
table for ADDRESS_TYPE='1'
, which is a street address
Some also have a Address_Type = '2'
, which is sometimes just a campusmailbox
number and sometimes a real address
Most have an Address_Type = '3'
, which is email address.
Yes I agree. That is a poor database design in the source system.
I have a data structure like this that I need to populate:
PATRON name, primary address street city state zip (Address_type 1), secondary address street city state zip (Address_Type 2) and an email field.
Note that, while the underlying database is Oracle, in my current role I am only able to access the data through MS Access 2007 (bleah!). So things I might know I can do in Oracle I assume won't work in Access SQL.
So I need to pull rows from the same table, PATRON_ADDRESS
, based on the patron_id
field. Is this query below
For every patron_ID
and address_type
in the Patron
Table, I need to conditionally select column values from the Patron_Address
table. Is this query going to do what I need?
Select P.PATRON.P.PATRON_ID,P.PATRON.LAST_NAME,
A1.StrretAddress_line1,A1.StreetAddress_Line2,etc.,A2.StreetAddress_Line1, A2.StreetAddress_Line2, A3.StreetAddress_Line1
from PATRON P, PATRON_ADDRESS A1, PATRON_ADDRESS A2, PATRON_ADDRESS A3
where (P.PATRON_ID = A1.PATRON_ID and
A1.ADDRESS_TYPE = '1')
and
(P.PATRON_ID = A2.PATRON_ID and
A2.ADDRESS_TYPE = '2')
and
(P.PATRON_ID = A3.PATRON_ID and
A3.ADDRESS_TYPE = '3');
I realize that this will be rather inefficient, but I don't know how I would reference the individual columns if I did an inner JOIN of PATRON_ADDRESS
for the address type = 1
data, and two outer joins of PATRON_ADDRESS
to get the address type = 2
and address_type = 3
data respectively. Thanks.
Upvotes: 0
Views: 1436
Reputation: 47402
You should be able to just JOIN
the table in for each address. It's very similar to what you have, but you really should be using JOIN
syntax, not listing all of your tables in the FROM
clause. That form of joining tables isn't clear, can't accomplish what the JOIN
clause can, and has been falling (has fallen) out of use for 20 years.
SELECT
P.patron_id,
P.last_name,
PA1.streetaddress_line1,
...
PA2.streetaddress_line2,
...
PA3.streetaddress_line3
FROM
Patron P
LEFT OUTER JOIN Patron_Address PA1 ON
PA1.patron_id = P.patron_id AND
PA1.address_type = '1'
LEFT OUTER JOIN Patron_Address PA2 ON
PA2.patron_id = P.patron_id AND
PA2.address_type = '2'
LEFT OUTER JOIN Patron_Address PA3 ON
PA3.patron_id = P.patron_id AND
PA3.address_type = '3'
If you know that everyone has an address of type 1 then you could make that an INNER JOIN
.
Upvotes: 1