S
sparkane
I have two tables: Customers and Donations. I want to create a query of
Donation_Amt, Donation_Date, and Donation_Purpose that contains only the
most recent donations with purposes that fall under the "DUES" category,
by Customer ID number.
I can set this up using two queries no problem:
q1:
SELECT Cust_ID, Max(date_paid) AS LastDuesDate
FROM qwoDONATION
WHERE purpose Like "*dues*"
GROUP BY Cust_ID
q2:
SELECT qD.Cust_ID, qD.purpose, qD.date_paid, qD.amount_paid
FROM qwoDONATION qD INNER JOIN q1 ON (qwoDONATION.Cust_ID = q1.Cust_ID)
AND (qwoDONATION.date_paid = q1.LastDuesDate)
WHERE qD.purpose Like "*dues*"
(The WHERE clause in q2 is necessary to remove non-dues-related payments
made by a customer on the same date as their most recent dues payment.)
Now I'm trying to turn this into a single query, and I'm having no luck.
Basically all my attempts either return multiple dues payments per
customer, not just the most recent, or return nothing at all. Here's
are some examples of what I've tried:
basic consolidated query:
SELECT Cust_ID, purpose, date_paid, amount_paid
FROM qwoDONATION
where clause 1:
WHERE purpose Like "*dues*"
AND date_paid in(select max(date_paid) from qwoDONATION where purpose
like "*dues*")
-> returns only those records with the most recent dues payment date in
the entire donations table. Obviously wrong.
where clause 2:
WHERE purpose Like "*dues*"
AND date_paid in(select max(date_paid) from qwoDONATION where purpose
like "*dues*" GROUP BY Cust_ID)
-> returns records for each customer, but will return records other than
the most recent. When I run the subquery by itself, however, I get only
one date (the most recent) per customer. The problem is that some
customers' previous dues payments have dates identical to other
customers' most recent dues payments; so those payments also are
included.
The trick seems to require tying the ID result in the subquery to the ID
result in the main query. Is there a simple way to get Access SQL to do
this? Thanks for any help.
spark
Donation_Amt, Donation_Date, and Donation_Purpose that contains only the
most recent donations with purposes that fall under the "DUES" category,
by Customer ID number.
I can set this up using two queries no problem:
q1:
SELECT Cust_ID, Max(date_paid) AS LastDuesDate
FROM qwoDONATION
WHERE purpose Like "*dues*"
GROUP BY Cust_ID
q2:
SELECT qD.Cust_ID, qD.purpose, qD.date_paid, qD.amount_paid
FROM qwoDONATION qD INNER JOIN q1 ON (qwoDONATION.Cust_ID = q1.Cust_ID)
AND (qwoDONATION.date_paid = q1.LastDuesDate)
WHERE qD.purpose Like "*dues*"
(The WHERE clause in q2 is necessary to remove non-dues-related payments
made by a customer on the same date as their most recent dues payment.)
Now I'm trying to turn this into a single query, and I'm having no luck.
Basically all my attempts either return multiple dues payments per
customer, not just the most recent, or return nothing at all. Here's
are some examples of what I've tried:
basic consolidated query:
SELECT Cust_ID, purpose, date_paid, amount_paid
FROM qwoDONATION
where clause 1:
WHERE purpose Like "*dues*"
AND date_paid in(select max(date_paid) from qwoDONATION where purpose
like "*dues*")
-> returns only those records with the most recent dues payment date in
the entire donations table. Obviously wrong.
where clause 2:
WHERE purpose Like "*dues*"
AND date_paid in(select max(date_paid) from qwoDONATION where purpose
like "*dues*" GROUP BY Cust_ID)
-> returns records for each customer, but will return records other than
the most recent. When I run the subquery by itself, however, I get only
one date (the most recent) per customer. The problem is that some
customers' previous dues payments have dates identical to other
customers' most recent dues payments; so those payments also are
included.
The trick seems to require tying the ID result in the subquery to the ID
result in the main query. Is there a simple way to get Access SQL to do
this? Thanks for any help.
spark