简体   繁体   English

根据其他值从多行中选择同一列

[英]select same column from multiple rows based on other values

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. 结果集中的列必须包含1-3种地址类型的值。 This is a typical 1 Patron to 1,3 Patron Address relation. 这是一个典型的1 Patron到1,3 Patron Address关系。

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.) (这是为了将系统从ILS转换到我们的学术图书馆中的另一个库。)

Everyone, from what I can tell, has a row in the PATRON_ADDRESS table for ADDRESS_TYPE='1' , which is a street address 据我所知,每个人在PATRON_ADDRESS表中PATRON_ADDRESS ADDRESS_TYPE='1' ,这是街道地址

Some also have a Address_Type = '2' , which is sometimes just a campusmailbox number and sometimes a real address 有些还具有Address_Type = '2' ,有时只是campusmailbox号码,有时是真实地址

Most have an Address_Type = '3' , which is email address. 大多数都具有一个Address_Type = '3' ,这是电子邮件地址。 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!). 请注意,虽然基础数据库是Oracle,但以我目前的角色,我只能通过MS Access 2007访问数据(麻烦!)。 So things I might know I can do in Oracle I assume won't work in Access SQL. 因此,我可能知道我可以在Oracle中做的事情,我认为在Access SQL中无法使用。

So I need to pull rows from the same table, PATRON_ADDRESS , based on the patron_id field. 因此,我需要根据patron_id字段从同一表PATRON_ADDRESS拉出行。 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. 对于Patron表中的每个patron_IDaddress_type ,我需要有条件地从Patron_Address表中选择列值。 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. 我知道这会是相当低效的,但我不知道我怎么会参考各个列,如果我做了一个内部联接的PATRON_ADDRESSaddress type = 1的数据,和两个外部连接PATRON_ADDRESS获得address type = 2address_type = 3数据。 Thanks. 谢谢。

You should be able to just JOIN the table in for each address. 您应该只需要为每个地址JOIN表即可。 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. 它与您所拥有的非常相似,但是您实际上应该使用JOIN语法,而不是在FROM子句中列出所有表。 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. 这种JOIN表的形式尚不清楚,无法完成JOIN子句可以完成的工作,并且已经(已经被淘汰)了20年。

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 . 如果您知道每个人的地址类型均为1,则可以将其INNER JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM