Skip to content
Advertisement

SQL Select results based on two rows with different condition

I’m trying to fetch results where I want to check if two rows exist for a common ForeignKeyId and add a check on both rows.

Suppose I have a table below – ‘Test1’

I want to fetch Result from Test2 table where Test1 should contain two rows where C = x and D <> NULL.

So, select query should return Result1 and Result2, not Result3

I tried:

Advertisement

Answer

To compare the same value in a different row a self join is used. In this case There are two Test1 tables, the first (Tc) matches the C criteria, and the second Td matches the D criteria.

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