Sorting Problem
Date: Fri, 31 May 2002 01:08:35 GMT
Message-ID: <n2AJ8.1021$Zo1.237699_at_twister.maine.rr.com>
I have a database to track annual gifts to a local non-profit. The gifts are recorded in a table:
CREATE TABLE AnnualGift (
DonorID INTEGER REFERENCES Donor(DonorID),
FiscalYear INTEGER,
Amount INTEGER
);
It is assumed that there is only one gift per donor to the annual fund each year. The table has records going back many years.
Management has asked for a report of all donors who did NOT make a report this year (FY 2002), but did make a gift in either (or both) of the last two years, sorted by the size of the gift in 2001. I'm having trouble getting the sorting right.
The query I created was this:
SELECT AG1.DonorID, AG1.FiscalYear, AG1.Amount FROM AnnualGift AG1
WHERE AG1.FiscalYear = 2001 OR AG1.FiscalYear = 2000 AND
NOT EXISTS (SELECT * FROM AnnualGift AG2 WHERE FiscalYear = 2002 AND AG1.DonorID =AG2.DonorID)
ORDER BY AG1.Amount DESC;
This gives me the right data (all 2000 or 2001 records where there is no 2002 record for that donor), but sorts by amount, so all the 2000 and 2001 records are mixed up.
How do I sort so that the 2001 records are sorted by amount, but the 2000 records for the same donor follow immediately after? I tried joining the table to itself to try to get the amount from 2001 appended to all records (regardless of FiscalYear) as another column so that I could sort on that, but I could not get it to work.
Your help is greatly appreciated!
- Bob Swerdlow