declare @YourTable table (
id int,
TestName varchar(10),
q1 char(1),
q2 char(1),
q3 char(1),
q4 char(1)
)
insert into @YourTable
(id, TestName, q1, q2, q3, q4)
select 1,'test1','1','2','1','1' union all
select 2,'test1','2','3','4','1' union all
select 3,'test2','1','1','4','2' union all
select 4,'test1','2','5','3','4' union all
select 5,'test2','1','5','2','4'
select TestName,
sum(case when q1 = '1' then 1 else 0 end) as [q1(1)],
sum(case when q1 = '2' then 1 else 0 end) as [q1(2)],
sum(case when q4 = '4' then 1 else 0 end) as [q4(4)],
sum(case when q4 = '5' then 1 else 0 end) as [q4(5)]
from @YourTable
group by TestName
source
share