Hi all, thanks for the useful replies.
I did get sent some info from capita support, which hopefully will help others, though the script above seems to be very comprehensive and time saving - cheers @password1234
The issue in this school was fixed when I added the SQL rules below. Still don't understand how it had worked for so long, then suddenly stopped - but such is life.