0

I have two example tables:

TABLE1:
      ID    | COLUMN_B| COLUMN_C
   _________|_________|_________
       1    |    0    |    1
    ________|_________|_________
       2    |    0    |    1
    ________|_________|_________
       3    |    0    |    1


    TABLE2:
      ID    | COLUMN_E| COLUMN_F
    ________|_________|________
       1    |    Y    |    X
    ________|_________|_________
       2    |    Y    |    X
    ________|_________|_________
       3    |    Y    |    X

They relate to each other with the ID column. I would like to perform a select on TABLE1, bringing COLUMN_B and bringing COLUMN_C only if the value on COLUMN_E is Y.

Is it possible to create a condition that simulates something like: "select column_b, and select column_c IF AND ONLY IF the value on column_e from TABLE2 is Y for the same ID"?

Nicole
  • 107
  • 1
  • 2
  • 12

2 Answers2

1

If I understand correctly, this is just a case expression:

select t1.column_b,
       (case when t2.column_e = 'Y' then t1.column_c end) as column_c
from table1 t1 left join
     table2 t2
     on t1.id = t2.id;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

use join

    select t1.COLUMN_B,
   case when t2.COLUMN_E='Y' then t1.COLUMN_C else null
end as c from table1 t1 
    left join table2 t2 on t1.id=t2.id
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
  • Unfortunately that will not solve the problem :( This query won't bring me neither column_b nor column_c when column_e != Y. I would like it to always bring column_b, but bring column_c only when column_e = Y. – Nicole Sep 20 '18 at 18:08
  • @Nicole then i got wrong actually case when will solve your problem – Zaynul Abadin Tuhin Sep 20 '18 at 18:10