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

  • Home
  • SEARCH
  • 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 7419847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:08:26+00:00 2026-05-29T08:08:26+00:00

I have a rather complex query that pretty much mimics a test query I

  • 0

I have a rather complex query that pretty much mimics a test query I have below:

SELECT C.*
  FROM Customer C 
       INNER JOIN CustDetail CD ON C.CustomerId = CD.CustomerId
       INNER JOIN Address A ON CD.DetailID = A.DetailID
       INNER JOIN Group G ON C.CustomerId = G.CustomerId   --Join only when C.code = 1
       INNER JOIN GroupDetail D ON G.GroupId = D.DetailId  --Join only when C.code = 1
WHERE G.Active = 1 AND        --Only when C.code = 1
      D.code = '1' AND        --Only when C.code = 1
      C.Id = @customerId

I’d like to do INNER JOINs on Group G and GroupDetail D (and ofcourse not have them in the WHERE conditions based on the table column C.code = 1

I replaced the INNER JOINs with LEFT OUTER JOINs for both the join conditions, but the result set is not what was expected

How do I conditionally do the JOIN

  • 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-29T08:08:27+00:00Added an answer on May 29, 2026 at 8:08 am
    SELECT C.*
      FROM Customer C 
           INNER JOIN CustDetail CD ON C.CustomerId = CD.CustomerId
           INNER JOIN Address A ON CD.DetailID = A.DetailID
           LEFT OUTER JOIN Group G ON C.CustomerId = G.CustomerId
           LEFT OUTER JOIN GroupDetail D ON G.GroupId = D.DetailId
    WHERE ((G.Active = 1 AND C.code = 1) OR G.Active IS NULL) AND
          ((D.code = '1' AND C.code = 1) OR D.code IS NULL) AND
          C.Id = @customerId
    

    I’m guessing you didn’t include the IS NULL checks before so you never got to see rows where C.code <> 1 ?

    You should check for NULL on a field that will never be null. This is almost always ‘id’, but it’s not clear that you have a G.id or a D.id.

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

Sidebar

Related Questions

I have this rather complex query that grabs data from three tables, and now
I have a set of rather complex ORM modules that inherit from Class::DBI .
I have a rather complex SQL query that I am looking for a little
I have a relatively complex query that I'm running. I've included it below, but
I have a number rather large, complex xml documents that I need to loop
I have a PHP class that stores a complex multidimensional array, and rather than
I am inside oracle procedure I have a complex query with join which gives
I have a legacy class that is rather complex to maintain: class OldClass {
I need to make a rather complex query, and I need help bad. Below
I have a fairly complex query in SQL that performs a count across two

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.