-2

How do I join on same column for different values and both the values should be and condition?

eg: TABLEA.COLUMNA='XYZ' AND TABLEB.COLUMNA='PQR'

Please note TableA and TableB are same table.

APC
  • 144,005
  • 19
  • 170
  • 281
ketan
  • 1
  • 2
    It is not clear what you are asking. The `ON` clause you showed us could be part of a valid self join query, but since you did not show the entire query or any data, this is not certain. – Tim Biegeleisen Oct 12 '17 at 05:47

1 Answers1

0

From your tagging you know need a self-join, in which two instances of the table are joined to each other. That's as simple as you would hope it would be:

select a.id as a_id
       , b.id as b_id
from your_table a
     cross join your_table b
where a.columnA = 'XYZ'
and b.columnA = 'PQR'

This query doesn't make sense logically. The cross join will produce a product of all the permutations of a.id, b.id. You probably don't want that. Maybe you want something like this:

select a.id as a_id
       , b.id as b_id
from your_table a
     join your_table b
       on a.some_other_column = b.some_other_column
where a.columnA = 'XYZ'
and b.columnA = 'PQR'

But you didn't post all your criteria so only you know for sure.

APC
  • 144,005
  • 19
  • 170
  • 281