--测试--测试数据 create table 表(分数 int,班级 varchar(10),姓名 varchar(10)) insert 表 select 60,'1班','张三' union all select 40,'1班','李四' union all select 60,'1班','王五' union all select 50,'2班','刘一' union all select 20,'2班','吴涛' union all select 43,'2班','将二' union all select 30,'3班','李力' union all select 79,'3班','小煤' union all select 80,'3班','刘涛' go--查询 select a.*,所在班级总分=isnull(b.总分,'') from 表 a left join( select 班级,位置=(count(*)+1)/2,总分=cast(sum(分数) as varchar) from 表 group by 班级 )b on a.班级=b.班级 and b.位置=( select count(*) from 表 where 班级=a.班级 and 姓名<=a.姓名) order by a.班级,a.姓名go--删除测试 drop table 表/*--测试结果 分数 班级 姓名 所在班级总分 ----------- ---------- ---------- ------------------------------ 40 1班 李四 60 1班 王五 160 60 1班 张三 43 2班 将二 50 2班 刘一 113 20 2班 吴涛 30 3班 李力 80 3班 刘涛 189 79 3班 小煤 (所影响的行数为 9 行) --*/
create table 表(分数 int,班级 varchar(10),姓名 varchar(10))
insert 表 select 60,'1班','张三'
union all select 40,'1班','李四'
union all select 60,'1班','王五'
union all select 50,'2班','刘一'
union all select 20,'2班','吴涛'
union all select 43,'2班','将二'
union all select 30,'3班','李力'
union all select 79,'3班','小煤'
union all select 80,'3班','刘涛'
go--查询
select a.*,所在班级总分=isnull(b.总分,'')
from 表 a
left join(
select 班级,位置=(count(*)+1)/2,总分=cast(sum(分数) as varchar)
from 表 group by 班级
)b on a.班级=b.班级 and b.位置=(
select count(*) from 表
where 班级=a.班级 and 姓名<=a.姓名)
order by a.班级,a.姓名go--删除测试
drop table 表/*--测试结果
分数 班级 姓名 所在班级总分
----------- ---------- ---------- ------------------------------
40 1班 李四
60 1班 王五 160
60 1班 张三
43 2班 将二
50 2班 刘一 113
20 2班 吴涛
30 3班 李力
80 3班 刘涛 189
79 3班 小煤 (所影响的行数为 9 行)
--*/