I am trying to retrieve in my result list a list of IDs where two conditions are satisfied: 1) the ID has a service (determined by referral or note) within a specified time 7/1/2009-6/30/2010 2) the ID has not appeared before the specified time
I've come up with this long query but it still brings back those that have a previous service:
SELECT DISTINCT c.id,
c.lastname,
c.firstname
FROM roundtable rt
INNER JOIN clients c
ON c.id = rt.clientid
LEFT OUTER JOIN notes n
ON c.id = n.clientid
LEFT OUTER JOIN referral rf
ON c.id = rf.clientid
WHERE ( rf.referraldate>='2009-07-01'
AND rf.referraldate<='2010-06-30' )
OR ( n.createddate>='2009-07-01'
AND n.createddate<='2010-06-30' )
AND c.id NOT IN (SELECT DISTINCT clt.id
FROM roundtable rtb
INNER JOIN clients clt
ON clt.id = rtb.clientid
LEFT OUTER JOIN notes nts
ON clt.id = nts.clientid
LEFT OUTER JOIN referral ref
ON clt.id = ref.clientid
WHERE ( rf.referraldate < '2009-07-01' )
OR ( n.createddate < '2009-07-01' ))
ORDER BY c.lastname,
c.firstname
For example: ID, ReferralDate, NoteCreatedDate
4, 2/12/2008, 3/12/2008
4, 7/15/2009, 7/30/2009
6, 5/30/2008, 2/26/2007
8, 7/20/2009, 3/20/2008
9, 7/20/2009, 10/3/2009
So IDs 4, 6 and 8 should not be in the return list since ID 4 has a previous referral and note outside the time period, and ID 6 has both referral and notes outside the time period while ID 8 has one note outside the time period. In this case, ID 9 should be the only one returned since it has dates in the time period and no previous records.
Thanks!