试试这个
drop table tests
CREATE TABLE tests(TestName varchar(10),Stage1 varchar(10),Stage2 varchar(10),Stage3 varchar(10),Stage4 varchar(10))
INSERT INTO tests
VALUES('Test1','John','Calra','John','Calra'),('Test2','Calra',null,'John','Calra')
select * from tests
select 'John' as [User]
,SUM(case when stage1='John' then 1 else 0 end) as stage1
,SUM(case when stage2='John' then 1 else 0 end) as stage2
,SUM(case when stage3='John' then 1 else 0 end) as stage3
,SUM(case when stage4='John' then 1 else 0 end) as stage4
from tests
UNION
select 'Calra' as [User]
,SUM(case when stage1='Calra' then 1 else 0 end) as stage1
,SUM(case when stage2='Calra' then 1 else 0 end) as stage2
,SUM(case when stage3='Calra' then 1 else 0 end) as stage3
,SUM(case when stage4='Calra' then 1 else 0 end) as stage4
from tests