Hi I'm not sure on field names and how your database works etc, but hopefully you'll be able to figure out what you need from the following.
This won't exclude records that have had treatment or have been returned, instead it will return all animals and flag where they've been returned or had treatment - this will allow you to filter them out using Crystal Record select.
Hopefully that all makes sence.
SELECT *
FROM Kennel k
JOIN Animal a
ON k.animal_id = a.animal_id
AND k.outcome_type='ADOPTION'
JOIN person p
ON k.owner_id = p.person_id
LEFT JOIN (
SELECT
k.animal_id,
k.intake_date,
k.intake_time,
'Returned' [Returned]
FROM Kennel k
WHERE k.intake_type='RETURN') r
ON r.animal_id = a.animal_id
AND r.intake_date + r.intake_time > k.outcome_date+k.outcome_time
LEFT JOIN (
SELECT
k.animal_id,
k.treatment_date,
k.treatment_time,
'Treatment Given' [Treatment]
FROM Kennel k
WHERE k.field = 'TREATMENT') t
ON t.animal_id = a.animal_id
AND t.treatment_date + t.treatment_time > r.intake_date+r.intake_time
The part in bold is where I'm not sure whether we should be comparing the treatment date with the intake_date of the return or with something in the main table.
Regards,
Ryan.
Edited by rkrowland - 29 Mar 2012 at 5:09am