join - sql server select from two tables as null or value -


this shouldn't hard somehow unable make work. have 2 tables: splanning_buildings , splanning_roomdata. primary key of objectid in splanning_buildings table represented objectid_bldg in splanning_roomdata table.

what need select rows splanning_buildings table in such way if value found inside splanning_roomdata table show value "existing" string; if not found in splanning_roomdata show blank.

this sql server 2012.

here sql:

select t1.objectid, name, t2.building_n  splanning_buildings t1 left join splanning_roomdata t2 on t1.objectid = t2.objectid_bldg t2.objectid_bldg null  select t1.objectid, name, t2.building_n  splanning_buildings t1 left join splanning_roomdata t2 on t1.objectid = t2.objectid_bldg  

the first query returns rows splanning-buildings table except row exists splanning_roomdata table; t2.building_n 'null'. makes sense.

the second query returns rows buildings table multiple t2.buildings_n rows.

none of these desired results! guess need kind of sql case() function?

any idea? thanks!

  select t1.objectid, name,         case when (exists(select top(1) 1 splanning_roomdata t2     t1.objectid = t2.objectid_bldg ))          'exists'         else 'not exists' end [status]     splanning_buildings t1 

Comments