Need MySQL query to run conditions both individually and as a group-possible? -


i have query attempts select cases/clients (clients.id) based on multiple conditions individually , group in 2 different tables (clients , referrals). >since clients can have multiple referrals, need query "loop" through each referral until 2 conditions satisfied , compare results group against third condition , return cases only. problem is running through each condition separately though they're >specified in 'where'. i've tried combining conditions in 'c.id not in' still cannot consider conditions together.

to clarify more: want referrals cases/clients satisfy these 3 conditions:

  1. each referral within time frame (r.referraldate>='2010-01-01' , r.referraldate<='2010-03-31')
  2. each referral agency except agency 16, 17 , 19 (r.agencyid<>16 , r.agencyid<>17 , r.agencyid<>19)
  3. and referrals case/client linked or referralcodeid = 18

select c.id, c.lastname, c.firstname, a.agencylabel 'referral made to:', r.referraldate, r.eligibilitydate, r.linkagedate, r.closuredate, rc.referralcode  clients c inner join referral r on c.id=r.clientid inner join agencies on a.agencyid=r.agencyid left outer join referralcodes rc on r.referralcodeid=rc.referralcodeid (r.referraldate>='2010-01-01' , r.referraldate<='2010-03-31')      and(r.agencyid<>16 , r.agencyid<>17 , r.agencyid<>19)     , (c.id not in (select clientid referral referralcodeid <> 18))    order c.id 

been working on while leads appreciated!

are wanting 1 case/client per row? or 1 referral? why wouldn't referrals have referralcode? etiher way don't think need left join anyway.

select * client c     join referral r on id=r.clientid      join agencies using(agencyid)       (r.referraldate >= '2010-01-01' , r.referraldate <='2010-03-31')      , r.agencyid not in(16,17,19)      , c.id not in (select clientid referral referrallcode <> 18 , r.referraldate >= '2010-01-01' , r.referraldate <='2010-03-31' ) group c.id; 

take off group if want referral

edit: changed check referral code reflect clarification op


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -