declare @docs table(id int, title varchar(20), [text] varchar(20))
insert into @docs(id, title, [text])
select 1, 'Тема дня', 'text' union all
select 2, 'Договор', 'text' union all
select 3, 'Сотрудники', 'text' union all
select 4, 'Статья о Васе', 'text'
declare @doc_group_relation table(id int, doc_id int, group_id varchar(10))
insert into @doc_group_relation(id, doc_id, group_id)
select 1, 1, 'g12' union all
select 2, 1, 'g13' union all
select 3, 2, 'g12' union all
select 4, 4, 'g13'
select title
,case when exists (select 1
from @doc_group_relation as r
where r.doc_id = d.id
and group_id = 'g12')
then 'true'
else 'false'
end as flag
from @docs as d
title flag
-------------------- -----
Тема дня true
Договор true
Сотрудники false
Статья о Васе false
(4 row(s) affected)