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 951095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:40:32+00:00 2026-05-15T23:40:32+00:00

I need to JOIN a table when certain conditions are met and LEFT JOIN

  • 0

I need to JOIN a table when certain conditions are met and LEFT JOIN it when other conditions are met. Since I am in a MS Dynamic CRM with filters, I can’t use UNION

   SELECT stuff.stuffs
     FROM MainTable    

JOINS othertable
LEFT JOIN othertables

LEFT JOIN TableX as X1 ON (Conditions1.1 = X1.1
                      AND MainTable.Type = 1)    
     JOIN TableX as X2 ON (Conditions2.2 = X2.2
                      AND MainTable.Type = 2)
  • If I comment out the X2 part, I get what I need from the X1 part and nothing from the X2.
  • If I LEFT JOIN the X2 part, I have extra information in X2.
  • If I leave it as such, I get what I need from the X2 part, but nothing from X1.

I tried a few variants, but I can’t reach a satisfactory solution.

EDIT: My original query was as such:

SELECT stuff.stuffs
     FROM MainTable

(LEFT) JOIN TableX as X1 
         ON (Conditions1.1 = X1.1
             AND MainTable.Type = 1)    
     OR 
         ON (Conditions2.2 = X2.2
             AND MainTable.Type = 2)

But if it is as left join, I get extra info from X2
and a JOIN give me missing info from X1,

  • 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-15T23:40:33+00:00Added an answer on May 15, 2026 at 11:40 pm

    The actual matching needed is not clear in the OP, but you could try something like:

    Select stuff.stuffs
    From MainTable
        Left Join TableX As X1
            On ( MainTable.Type = 1 And X1.1 = ...)
                Or ( MainTable.Type = 2 And X1.2 = ...)
    

    Given what you have added to the OP and in comments, it is still not clear whether you seek rows from TableX that satisfy either condition or one and only one of the conditions. However, for completeness here’s both:

    Either condition: My original solution above and the solution you added to your post.

    One and only one of the conditions:

    Select stuff.stuffs
    From MainTable
        Left Join TableX As X1
            On X1.1 = ...
        Left Join TableX As X2
            On X2.2 = ...
    Where ( MainTable.Type = 1 And X1.PK Is Not Null And X2.PK Is Null )
        Or ( MainTable.Type = 2 And X2.PK Is Not Null And X1.PK Is Null )
    

    What is missing from the original post is any notion of foreign keys references from TableX to MainTable. Thus, typically, I would have thought something like:

    Select stuff.stuffs
    From MainTable
        Left Join TableX As X1
            On X1.FK = MainTable.PK
        Left Join TableX As X2
            On X2.FK = MainTable.PK
    Where ( MainTable.Type = 1 And X1.Col1 = ...  And X2.PK Is Null )
        Or ( MainTable.Type = 2 And X2.Col1 = ... And X1.PK Is Null )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.