SQL fetching data when joining different columns depending of the data of one collumn

I want to get Type, Label1 Label2 and NUM

when Type from TABLE1 = Type Table 2 
AND if type from Table1 = x i want Label1 from table1 = Label from Table2
    If type from table1 = Y I want LAbl~el2 from table1 = Label from table2

enter image description here

ex:
If Table1 had Lable1 and Lable2 together in a collunm named Label I would do:

SELECT t1.type, t1.Label, t2.NUM
FROM Table1 t1
     Table2 t2
WHERE t1.type = t2.typ2 and
      t1.LABEL = t2. LABEL

But in my scenario I have two labels in table 1. Label 1 for type X and Label 2 for type Y.

So what’s the elegant and efficient way (assuming i have a lot of data in the table) to:

select the data t1.Type, t1.Label1 t1.Label2 and t2.NUM when:
t1.type = t2.type2 
and If t1.type = 'X' -> t1.Label1 = t2.Label
and If t1.type = 'Y' -> t1.label2 = t2.Label?

This should be the result of the select I want to make:
enter image description here

Thanks for you help.


Source: oracle

Leave a Reply