Here is a solution with pivot
.
Sample data
I added another submissionId
to the sample data to have an extra result row.
create table data
(
submissionId nvarchar(8),
itemId int,
answerValue nvarchar(20)
);
insert into data (submissionId, itemId, answerValue) values
('abcd1234', 7857, 'YES'),
('abcd1234', 2226, '34 some Street'),
('abcd5678', 7857, 'NO'),
('abcd5678', 2226, '100 east lane');
Solution
select piv.submissionId,
[7857] as Question1,
[2226] as Question2
from data d
pivot (max(d.answerValue) for d.itemId in ([7857], [2226])) piv;
Result
submissionId Question1 Question2
------------ --------- --------------
abcd1234 YES 34 some Street
abcd5678 NO 100 east lane
Fiddle to see things in action.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…