Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:52:49+00:00 2026-05-20T09:52:49+00:00

I have a query that attempts to select cases/clients (clients.id) based on multiple conditions

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:52:49+00:00Added an answer on May 20, 2026 at 9:52 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that is dynamically built after looking up a field list
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL
I have a query that is currently using a correlated subquery to return the
I have a query that has 7 inner joins (because a lot of the
I have a query that works on Postgresql 7.4 but not on Postgresql 8.3
I have a query that runs super fast when executed in the sql editor
I have a query that pulls up questions from one table and answers from
I have a query that I am generating the XML from in Oracle using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.