ppau2004
ppau2004

Reputation: 332

SQL Server computed column based on join

I have 2 tables:

Table 1:

id name
--------
1 Mark
2 Anna

Table 2:

id active_name
--------------
2 Anna

I want to have a 3rd table or view:

id name isActive
--------------------
1 Mark  No
2 Anna Yes

How do I do that in SQL Server.

Upvotes: 1

Views: 273

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270463

You can use left join and case expression:

select t1.id, t1.name,
       (case when t2.id is null then 'No' else 'Yes' end) as isActive
from table1 t1 left join
     table2 t2
     on t2.id = t1.id and t2.name = t1.name;

Upvotes: 4

Related Questions