Skip to content
Advertisement

Confused with Join logic

I find it so confused that my simple logic doesn’t work.

I have a data with 2 columns ID and code.

1 ID can link to multiple code.

So the sample data is like this:

The rule is that:

  • Find ID that has code = 1 AND code not equal to 2 AND code =3

So clearly a is fail, b is good, c is good, d is fail.

I have 2 approaches below and neither of them work!

Can you please help?

Many thanks,

Harry

Approach #1:

Approach #2: join my table 2 times

Advertisement

Answer

Welcome to S/O. Your posts will get better with time with reading others and additional feedback. Your question states one thing, but query offers another.

Your WHERE clause on its individual parts is asking for the code = 1, and the corresponding “ID” does not have a code = 2, but it DOES have a code = 3 IN ADDITION to code = 1.

Aside from aggregations, you can use the same table and self-join on the conditions you want. In this case, I might start with your base query of just the code = 1. Then I would join again on same ID, but it also having a code 3 (both 1 and 3 required). Then a third LEFT JOIN for code 2 and ensuring it is NOT found

Another approach is via aggregation, but getting a count of 1 & 3, another for code = 2 but making sure counts make sense to your needs.

In the above, say you have multiple records for a given ID that are code = 1, you MAY get a count higher than 1. Hence I am summing just for code 1 and only care if it has AT LEAST 1. Similar requirement for code = 3 having the sum of AT LEAST 1. Finally, the sum of code = 2 making sure it is = 0 for its count.

FEEDBACK on SECOND query from OP

The reason for your failure on the second query is in the first part before your join for code test = 3

You are querying where table1.code = 1 which is ok, and then your join from table1 to table2 is only on the ID. So, for example on your data with

your self join is returning records with codes 1, 2 AND 3. So even though you may not want #2, it is still pulling in IDs 1 & 3 which DO qualify, hence the logical failure. That is why in my FIRST example, I am doing a LEFT JOIN on same id and explicitly code = 2… but the where clause is explicitly EXCLUDING by where the table 3 alias ID is NULL, meaning it does NOT see a record for same ID, code = 2.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement