Вы должны использовать Pivot, чтобы написать этот запрос:
Sample Data:
DECLARE @Table1 TABLE( id int, name nvarchar(10));
DECLARE @Table2 TABLE( id int, Question nvarchar(10),Answer nvarchar(10))
insert into @Table1 (id,[name])values(1,'AAA')
insert into @Table1 (id,[name])values(2,'BBB')
insert into @Table2 (id,Question,Answer)values(1,'Q1','YES')
insert into @Table2 (id,Question,Answer)values(1,'Q2','NO')
insert into @Table2 (id,Question,Answer)values(1,'Q3','YES')
insert into @Table2 (id,Question,Answer)values(1,'Q4','NO')
insert into @Table2 (id,Question,Answer)values(1,'Q5','YES')
insert into @Table2 (id,Question,Answer)values(2,'Q1','YES')
insert into @Table2 (id,Question,Answer)values(2,'Q2','YES')
insert into @Table2 (id,Question,Answer)values(2,'Q3','YES')
insert into @Table2 (id,Question,Answer)values(2,'Q4','YES')
insert into @Table2 (id,Question,Answer)values(2,'Q5','YES')
select * from @Table1 t1
inner join (
select * from(
select
t2.id
,t2.Question
,t2.Answer
from @Table2 t2
)temp
pivot
(
MAX(Answer)
for question in ([Q1],[Q2],[Q3],[Q4],[Q5])
) as PivotTable
)TemporaryTable on t1.id=TemporaryTable.id
если ваши вопросы - Dynami c, вы можете написать так
Declare @cols nvarchar(MAX);
Declare @query nvarchar(MAX);
set @cols=( select STRING_AGG('['+Question+']',',') from @Table2);
set @query='
select * from @Table1 t1
inner join (
select * from(
select
t2.id
,t2.Question
,t2.Answer
from @Table2 t2
)temp
pivot
(
MAX(Answer)
for question in ('+@cols+')
) as PivotTable
)TemporaryTable on t1.id=TemporaryTable.id
'
exec sp_executesql @query