M
Mark Andrews
I must be brain dead today, here is a sql question maybe someone can help.
I have two tables (tblContact and tblDonation) and I'm trying to calculate
the
- firstdonationdate
- lastdonationdate
- LastDonationAmount
Using Min and max for first and last donation dates (works fine)
Trying to use First (with a DESC sort by Donationdate) to get the first
donation amount (which would be the LAST donation made by the contact).
This part is NOT working.
Example: Contact Joe Smith has 3 donations
July 3, 2009 $500
Aug 5, 2009 $600
Sep 7, 2009 $700
The last donation was $700
-------------------------
First try at sql:
-------------------------
SELECT tblContact.ContactID, Min(tblDonation.DonationDate) AS
FirstDonationDate, Max(tblDonation.DonationDate) AS LastDonationDate,
Nz(First([DonationAmount]),0) AS LastDonationAmount
FROM tblContact LEFT JOIN tblDonation ON tblContact.ContactID =
tblDonation.ContactID
GROUP BY tblContact.ContactID
ORDER BY Max(tblDonation.DonationDate) DESC;
-------------------------
second try qryDonationSortedByDate is tblDonation.* sorted desc by
Donationdate
-------------------------
SELECT tblContact.ContactID, Min(qryDonationSortedByDate.DonationDate) AS
FirstDonationDate, Max(qryDonationSortedByDate.DonationDate) AS
LastDonationDate, Nz(First([DonationAmount]),0) AS LastDonationAmount
FROM tblContact LEFT JOIN qryDonationSortedByDate ON tblContact.ContactID =
qryDonationSortedByDate.ContactID
GROUP BY tblContact.ContactID;
Thanks in advance,
Mark
I have two tables (tblContact and tblDonation) and I'm trying to calculate
the
- firstdonationdate
- lastdonationdate
- LastDonationAmount
Using Min and max for first and last donation dates (works fine)
Trying to use First (with a DESC sort by Donationdate) to get the first
donation amount (which would be the LAST donation made by the contact).
This part is NOT working.
Example: Contact Joe Smith has 3 donations
July 3, 2009 $500
Aug 5, 2009 $600
Sep 7, 2009 $700
The last donation was $700
-------------------------
First try at sql:
-------------------------
SELECT tblContact.ContactID, Min(tblDonation.DonationDate) AS
FirstDonationDate, Max(tblDonation.DonationDate) AS LastDonationDate,
Nz(First([DonationAmount]),0) AS LastDonationAmount
FROM tblContact LEFT JOIN tblDonation ON tblContact.ContactID =
tblDonation.ContactID
GROUP BY tblContact.ContactID
ORDER BY Max(tblDonation.DonationDate) DESC;
-------------------------
second try qryDonationSortedByDate is tblDonation.* sorted desc by
Donationdate
-------------------------
SELECT tblContact.ContactID, Min(qryDonationSortedByDate.DonationDate) AS
FirstDonationDate, Max(qryDonationSortedByDate.DonationDate) AS
LastDonationDate, Nz(First([DonationAmount]),0) AS LastDonationAmount
FROM tblContact LEFT JOIN qryDonationSortedByDate ON tblContact.ContactID =
qryDonationSortedByDate.ContactID
GROUP BY tblContact.ContactID;
Thanks in advance,
Mark