开发者

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

开发者 https://www.devze.com 2023-02-14 05:13 出处:网络
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 multi

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 开发者_StackOverflow社区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!


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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消