create talbe score (姓名 varchar(14),科目 varchar(14),分數 int)
insert into score select '張三','數學',85
union all select '張三','語文',90
union all select '張三','英語',88
union all select '李四','數學',87
union all select '李四','語文',86
union all select '李四','英語',92
union all select '王五','數學',90
union all select '王五','語文',78
union all select '王五','英語',88
select 姓名,sum( case when 科目='數學' then 分數 end ) as 數學,
sum( case when 科目='語文' then 分數 end ) as 語文,
sum( case when 科目='英語' then 分數 end ) as 英語,sum(分數) as 總分 into #s from score group by 姓名 order by 總分 desc
select *,(select count(distinct 總分) from #s where 總分>=a.總分) as 排名
from #s a
order by 總分 desc