I have a SQL statement that I can’t seem to solve for… I’m not sure how to perform an “OR” on my join. In fact, I’m not sure if I should even be doing a join at all… Here is what I have so far:
SELECT o.* FROM dbo.Orders o
INNER JOIN dbo.Transactions t1 ON t1.OrderId = o.OrderId
AND t1.Code = 'TX33'
INNER JOIN dbo.Transactions t2 ON t2.OrderId = o.OrderId
AND t2.Code = 'TX34'
WHERE o.PurchaseDate NOT NULL
I haven’t ran this yet, but I assume that this will get me all orders that have a purchase date that also have BOTH TX33 and TX34 transactions. Any orders without both of those transactions won’t show up (due to the INNER JOINs). The part I’m stuck at is this:
I need to be able to also ensure that the order also contains either:
- TX35 AND TX36
- TX37
- TX38 AND TX39
Only one of those additional conditions is necessary. I know I can’t simply INNER JOIN because that means it’s required to be there. If I do a regular JOIN I could possibly make it work if one of the ‘OR’ conditions wasn’t itself an ‘AND’ condition (I’m not sure how to do TX35 AND TX36 as one JOIN condition, nor TX38 AND TX39.
You could also write it like this: