The following SQL is my solution to a query a user had, but I'm unsure if this was a "Best Practice" SQL solution. I used a subquery to find what they were looking for.
The user wanted a list of people who made donations in 2004 but haven't yet in 2005. They didn't want to send reminders to people who have already contibuted. The user wanted to create a mailing list from this query.
I used the MAX function to find the last donation made and excluded the 2005 records.
It seems like there should be simpler solution than the one I came up with.
SQL Server 2000 Code:
SELECT Main.FirstName, Main.LastName, Main.Address1, Main.Address2, Main.City, Main.State, Main.Zip, Donor.GiftAmt, Donor.DonationPmtDate,
Donor.FundSource, Main.Deceased,
(SELECT MAX(DatePart(year, D.donationpmtdate))
FROM Donor AS D
WHERE D.MasterID = Donor.MasterID) AS DateCheck
FROM Main LEFT OUTER JOIN
Donor ON Main.MasterID = Donor.MasterID
WHERE (Donor.GiftAmt >= 0) AND (Donor.DonationPmtDate BETWEEN CONVERT(DATETIME, '2004-01-01 00:00:00', 102) AND CONVERT(DATETIME,
'2005-12-31 00:00:00', 102)) AND
(((SELECT MAX(DatePart(year, D.donationpmtdate))
FROM Donor AS D
WHERE D.MasterID = Donor.MasterID)) <> 2005)
ORDER BY Main.LastName
The user wanted a list of people who made donations in 2004 but haven't yet in 2005. They didn't want to send reminders to people who have already contibuted. The user wanted to create a mailing list from this query.
I used the MAX function to find the last donation made and excluded the 2005 records.
It seems like there should be simpler solution than the one I came up with.
SQL Server 2000 Code:
SELECT Main.FirstName, Main.LastName, Main.Address1, Main.Address2, Main.City, Main.State, Main.Zip, Donor.GiftAmt, Donor.DonationPmtDate,
Donor.FundSource, Main.Deceased,
(SELECT MAX(DatePart(year, D.donationpmtdate))
FROM Donor AS D
WHERE D.MasterID = Donor.MasterID) AS DateCheck
FROM Main LEFT OUTER JOIN
Donor ON Main.MasterID = Donor.MasterID
WHERE (Donor.GiftAmt >= 0) AND (Donor.DonationPmtDate BETWEEN CONVERT(DATETIME, '2004-01-01 00:00:00', 102) AND CONVERT(DATETIME,
'2005-12-31 00:00:00', 102)) AND
(((SELECT MAX(DatePart(year, D.donationpmtdate))
FROM Donor AS D
WHERE D.MasterID = Donor.MasterID)) <> 2005)
ORDER BY Main.LastName