I have a bit of a scripting issue which I'm trying to work on and have got a bit stuck. I'm trying to update a few fields in a couple of tables and one of them I can't quite get my head around. This select statement... SELECT ID, start_date, end_date , (SELECT MAX(date_received) FROM _donation WHERE _donation.gift_aid_declaration_id = _Gift_Aid_Declaration.id AND _donation.gift_aid_status = 4) AS Final_Donation_Date FROM _Gift_Aid_DeclarationWHEREscan_file_ref is nulland event_id is nulland status = 1
Now, the problem is that I need to update the end_date so that it is the same as the Final_Donation_Date, which first of all sounded like a simple idea, but every time I try to come up with a cunning plan it seems to fall flat on it's face. The trouble is that because of the combination of:- Data in the 2 fields are in different tables (which wouldn't normally be a problem by using WHERE EXISTS)
- Because I have the MAX(date_received) in there.
If I try to use WHERE EXISTS: UPDATE DonationSET end_date = (SELECT max (date_received) FROM _donation JOIN _Gift_aid_declaration ON _Gift_aid_declaration.id = _donation.gift_aid_declaration_id WHERE _donation.gift_aid_declaration_id = _Gift_Aid_Declaration.id AND _donation.gift_aid_status = 4)WHERE EXISTS (SELECT max (date_received) FROM _donation JOIN _Gift_aid_declaration ON _Gift_aid_declaration.id = _donation.gift_aid_declaration_id WHERE _donation.gift_aid_declaration_id = _Gift_Aid_Declaration.id AND _donation.gift_aid_status = 4
But the bit where it fails is that because I'm now having to put a join in this select statement it is just returning the one MAX date for the entire table. I've now run into a bit of a brick wall and can't think how to get around this, so any help that you guys could provide would be hugely appreciated! Regards, Tim