sql server - How to write a T-Sql query that would output 1 if the value in a column in Table A matches the value in a column in Table B, 0 otherwise? -
i using sql server 2012 , need write query give me following output:
code value frbar 0 enspa 1 dewine 1 ... to achieve above, working 2 specific tables in database, namely table , table b. table has column called codea , table b has column called codeb.
i want write t-sql query match values in column codea in column codeb , ouput result mentioned above.
i know need join on these 2 columns confused how implement logic of returning 1 if there match , 0 otherwise.
you need use full outer join:
select case when codea not null codea else codeb end code, case when codea not null , codeb not null 0 else 1 end value tablea full outer join tableb on codea = codeb if there match query output 1 value. otherwise query output 0 along not null code value.
note: using left join check only in 1 direction, i.e. codes in 1 table don't exist in other table. if want check in both directions, have use full join.
Comments
Post a Comment