views:

73

answers:

2

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  A: 

I'm thinking you need to try this instead:

   SELECT stuff.stuffs
     FROM TABLEX x
LEFT JOIN MAINTABLE mt1 ON mt.1 = x.1
                       AND mt.type = 1
LEFT JOIN MAINTABLE mt2 ON mt.2 = x.2
                       AND mt.type = 2
OMG Ponies
I can't really do this since I have 9 other joins and left joins before the 2 I need to fix.
Mashuu
@Mashuu: We can only go by what you details you provide.
OMG Ponies
That is true, my apologies, I was expecting a simple one line answer or a special join property that I didn't know about. Thank you for helping me, it is greatly appreciated.
Mashuu
@Mashuu: Thanks for understanding, take care.
OMG Ponies
+1  A: 

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 )
Thomas
This solution gives me extra info from X2 which I musn't have. A JOIN, would create missing info from X1.
Mashuu
@Mashuu - Please provide some sample data and sample output that better illustrates what you are trying to achieve. It is not clear whether you want data matching both conditions, one and only one of the conditions, either condition etc.
Thomas
Thanks, using a variant of One and only one, I (You) managed to make it work! I only need to add a simple CASE in my SELECT to make everything work!
Mashuu
+1: Well done!!
OMG Ponies