2

I have 5 different tables T_DONOR, T_RECIPIENT_1, T_RECIPIENT_2, T_RECIPIENT_3, and T_RECIPIENT_4. All 5 tables have the same CONTACT_ID.

This is the T_DONOR table:

T_DONOR

T_RECIPIENT_1: enter image description here


T_RECIPIENT_2: enter image description here


This is what I want the final table to look like with more recipients and their information to the right.

enter image description here


T_RECIPIENT_3 and T_RECIPIENT_4 are the same as T_RECIPIENT_1 and T_RECIPIENT_2 except that they have different RECIPIENT ID and different names. I want to combine all 5 of these tables so on one line I can have the DONOR_CONTACT_ID which his information, and then all of the Recipient's information.

The problem is that when I try to run a query, it does not work because not all of the Donors have all of the recipient fields filled, so the query will run and give a blank table. Some instances I have a Donor with 4 Recipients and other times I have a Donor with only 1 Recipient so this causes a problem. I've tried running queries where I connect them with the DONOR_CONTACT_ID but this will only work if all of the RECIPIENT fields are filled. Any suggestions on what to do? Is there a way I could manipulate this in VBA? I only know some VBA, I'm not an expert.

4

2 回答 2

1

First I think you want all rows from T_DONOR. And then you want to pull in information from the recipient tables when they include DONOR_CONTACT_ID matches. If that is correct, LEFT JOIN T_DONOR to the other tables.

Start with a simpler set of fields; you can add in the "name" fields after you get the joins set to correctly return the rest of the data you need.

SELECT
    d.DONOR_CONTACT_ID,
    r1.RECIPIENT_1,
    r2.RECIPIENT_1
FROM
    (T_DONOR AS d
    LEFT JOIN T_RECIPIENT_1 AS r1
    ON d.ORDER_NUMBER = r1.ORDER_NUMBER)
    LEFT JOIN T_RECIPIENT_2 AS r2
    ON d.ORDER_NUMBER = r2.ORDER_NUMBER;

Notice the parentheses in the FROM clause. The db engine requires them for any query which includes more than one join. If possible, set up your joins in Design View of the query designer. The query designer knows how to add parentheses to keep the db engine happy.

Here is a version without aliased table names in case it's easier to understand and set up in the query designer ...

SELECT
    T_DONOR.DONOR_CONTACT_ID,
    T_RECIPIENT_1.RECIPIENT_1,
    T_RECIPIENT_2.RECIPIENT_1
FROM
    (T_DONOR
    LEFT JOIN T_RECIPIENT_1
    ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_1.ORDER_NUMBER)
    LEFT JOIN T_RECIPIENT_2
    ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_2.ORDER_NUMBER;
于 2013-07-16T17:25:57.810 回答
0
    SELECT T_DONOR.ORDER_NUMBER, T_DONOR.DONOR_CONTACT_ID, T_DONOR.FIRST_NAME, T_DONOR.LAST_NAME, T_RECIPIENT_1.RECIPIENT_1, T_RECIPIENT_1.FIRST_NAME, T_RECIPIENT_1.LASTNAME 
        FROM T_DONOR 
        JOIN T_RECIPIENT_1 
        ON T_DONOR.DONOR_CONTACT_ID = T_RECIPIENT_1.DONOR_CONTACT_ID

这向您展示了如何加入第一个收件人表,您应该能够为其他三个遵循相同的结构......

于 2013-07-16T17:07:24.300 回答