0

I have a query that attempts to select cases/clients (clients.id) based on multiple conditions individually and as a group in two different tables (clients and referrals). >Since clients can have multiple referrals, I need the query to "loop" through each referral until two conditions are satisfied and then compare those results as a group against the third condition and return those cases only. The problem is it is running through each condition separately even though they're all >specified in the 'where'. I've tried combining the conditions in the 'c.id not in' but still cannot get it to consider all conditions together.

To clarify some more: I want referrals for cases/clients that satisfy these three conditions:

  1. each referral is within a certain time frame (r.ReferralDate>='2010-01-01' and r.ReferralDate<='2010-03-31')
  2. each referral is from any agency except agency 16, 17 and 19 (r.AgencyID<>16 and r.AgencyID<>17 and r.AgencyID<>19)
  3. and ALL referrals for that case/client are successfully linked or referralcodeid = to 18

select c.id, c.lastname, c.firstname, a.AgencyLabel as 'Referral Made to:', r.ReferralDate, r.EligibilityDate, r.LinkageDate, r.ClosureDate, rc.referralcode 
from clients c
inner join referral r on c.id=r.ClientID
inner join agencies a on a.AgencyID=r.AgencyID
left outer join referralcodes rc on r.referralcodeid=rc.referralcodeid
where (r.ReferralDate>='2010-01-01' and r.ReferralDate<='2010-03-31')  
   and(r.AgencyID<>16 and r.AgencyID<>17 and r.AgencyID<>19) 
   and (c.id not in (select clientid from referral where referralcodeid <> 18))
   order by c.id

Been working on this for a while so any leads would be greatly appreciated!

2 Answers 2

0

Are you only wanting one case/client per row? Or one referral? Why wouldn't all referrals have a referralcode? Etiher way I don't think you need LEFT JOIN anyway.

SELECT * FROM client c
    JOIN referral r ON id=r.ClientID 
    JOIN agencies a USING(AgencyID) 
WHERE
     (r.ReferralDate >= '2010-01-01' and r.ReferralDate <='2010-03-31')
     AND r.AgencyID NOT IN(16,17,19)
     AND c.id NOT IN (SELECT ClientID FROM referral WHERE referrallcode <> 18 AND r.ReferralDate >= '2010-01-01' AND r.ReferralDate <='2010-03-31' )
GROUP BY c.id;

Take off GROUP BY if you want it by referral

Edit: Changed the check for the referral code to reflect the clarification by OP

7
  • You're right--I don't need a left join in this query. This answer was helpful in seeing how else to word the conditions. It still screens out cases/clients that have referrals outside that time period and where those referrals have referralcodeid<>18. I only want the referrals in that time period to be considered and then as a group against the last condition. This query is considering referrals outside that time period and screening out based on those referrals.
    – WixLove
    Mar 2, 2011 at 19:22
  • I guess I don't understand your question then. Your #1 says the referrals must fall within your time period. Of course it screens them out. Your #3 says you want all referrals tied to code 18. If they aren't tied together that way in the database then what's the point of a query?
    – Cfreak
    Mar 2, 2011 at 19:48
  • I stand corrected and apologize: I was confusing your suggestion with the previous one. The query you suggested doesn't consider ALL the referrals for each id, only separately. So it returns all ids with the conditions even though, for ex., c.id 538 has 4 referrals and 2 of them with code<>18. I don't want 538 to be returned since I want ALL referrals considered and to be =18. I want the condition to loop through each referral based on c.id. I hope this clarifies more.
    – WixLove
    Mar 2, 2011 at 20:37
  • Edited my query which should give you what you want.
    – Cfreak
    Mar 2, 2011 at 21:09
  • THAT completely worked! Didn't realize I had to specify the date twice--interesting. Thank you so much for your help--I am much obliged!
    – WixLove
    Mar 3, 2011 at 18:29
0

Take the third tier comparison out, group that query in parenthesis, and then select again from that using your third tier comparison in the where clause.

select * from (
select c.id, c.lastname, c.firstname, a.AgencyLabel as 'Referral Made to:', r.ReferralDate, r.EligibilityDate, r.LinkageDate, r.ClosureDate, rc.referralcode 
    from clients c
    inner join referral r on c.id=r.ClientID
    inner join agencies a on a.AgencyID=r.AgencyID
    left outer join referralcodes rc on r.referralcodeid=rc.referralcodeid
    where (r.ReferralDate>='2010-01-01' and r.ReferralDate<='2010-03-31')  
       and(r.AgencyID<>16 and r.AgencyID<>17 and r.AgencyID<>19) 
       order by c.id
) AS `query`
WHERE id not in (select clientid from referral where referralcodeid <> 18)
2
  • I didn't know you could do this! Nice. But it still screens out cases that have referrals outside the time period since those referrals <>18. Bummer.
    – WixLove
    Mar 2, 2011 at 19:24
  • I did that bid of code pretty fast. Now that you know you can separate them out, I bet you can find the right combination of conditions to get you what you want. Mar 2, 2011 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.