select t_student.学号,t_student.学生姓名,table1.荣誉分,table1.斑级
from t_student
left join
(select max(荣誉分) as num ,斑级
from t_student
group by 斑级) table1
on t_student.荣誉分=table1.num and t_student.斑级=table1.斑级where table1.荣誉分 is not null
from t_student
left join
(select max(荣誉分) as num ,斑级
from t_student
group by 斑级) table1
on t_student.荣誉分=table1.num and t_student.斑级=table1.斑级where table1.荣誉分 is not null
group by 学号,学生姓名,斑级
SELECT * FROM t_student INNER JOIN #J ON t_student.荣誉分=#J.荣誉分 AND t_student.班级=#J.班级
1.全部用聚合函数实现,代码如下:
select max(学号) 学号,max(学生姓名) 学生姓名,max(荣誉分) 荣誉分,max(斑级) 斑级 from t_student group by class2.还有一种,是要分部来实现的。第一步先创建一个虚拟表,然后在用表结合的方式实现,代码如下:
select a.学号,a.学生姓名, a.荣誉分, a.班级 from t_student a, (select max(学号) 学号,class from t_student group by class) b where a.学号 = b.学号 and a.班级 = b.班级都经过测试,可以实现
create table cj (name char (10),bj char (10),cj [decimal](12, 2) )
insert into cj (name,bj,cj)
select '张一','一班',100
union
select '张二','一班',80
union
select '张三','一班',40
union
select '张四','一班',75
union
select '张五','一班',99
union
select '张六','一班',56
union
select '李一','二班',20
union
select '李二','二班',99
union
select '李三','二班',60
union
select '李四','二班',45
union
select '李五','二班',66
union
select '李六','二班',28
------------
select * from cj a where a.name in (select top 1 name from cj where bj=a.bj
order by bj desc,cj desc) order by bj desc,cj desc
from t_student a,
(select 斑级,max(荣誉分) as 荣誉分 from t_student group by 斑级) b
where a.斑级=b.斑级 and a.荣誉分=b.荣誉分
select '张三',80,'001' union
select '李四',70,'002' union
select '王五',100,'001' union
select '赵六',90,'002'select * from t_student a where a.学生姓名 in (select top 1 学生姓名 from t_student where 斑级=a.斑级
order by 斑级 desc,荣誉分 desc) order by 斑级 学号 学生姓名 荣誉分 斑级
---- ------- ------ ----
2 王五 100 001
4 赵六 90 002
insert into t_student
select 1,'张三',80,'001' union
select 2,'李四',70,'002' union
select 3,'王五',100,'001' union
select 4,'赵六',90,'002'select * from t_student a where a.学生姓名 in (select top 1 学生姓名 from t_student where 斑级=a.斑级
order by 斑级 desc,荣誉分 desc) order by 斑级 学号 学生姓名 荣誉分 斑级
---- ------- ------ ----
3 王五 100 001
4 赵六 90 002
from t_student a,(select 班级,max(荣誉分) from t_student group by 班级)b
where a.班级=b.班级 and a.荣誉分=b.荣誉分
order by 班级
(select ryf2=max(ryf) from cj group by class)a
on cj.ryf=a.ryf2
insert cj select 1,'张三',80,'001'
union all select 2,'李四',70,'002'
union all select 3,'王五',100,'001'
union all select 4,'赵六',90,'002'select * from cj right join
(select ryf2=max(ryf) from cj group by class)a
on cj.ryf=a.ryf2drop table cj
create table kkk(学号 int,学生姓名 char(8),荣誉分 int,斑级 char(3))insert into kkk select 1,'张三',80,'001'
union select all 2,'李四',70,'002'
union select all 3,'王五',100,'001'
union select all 4,'赵六',90,'002'--测试
select t_student.* from t_student,(select max(荣誉分) as 最高荣誉分 ,斑级 from t_student group by 斑级) as b where t_student.荣誉分=b.最高荣誉分 and t_student.斑级=b.斑级--测试结果
学号 学生姓名 荣誉分 斑级
----------- -------- ----------- ----
4 赵六 90 002
3 王五 100 001(所影响的行数为 2 行)
--测试数据
create table t_student(学号 int,学生姓名 char(8),荣誉分 int,斑级 char(3))insert into kkk select 1,'张三',80,'001'
union select all 2,'李四',70,'002'
union select all 3,'王五',100,'001'
union select all 4,'赵六',90,'002'--测试
select t_student.* from t_student,(select max(荣誉分) as 最高荣誉分 ,斑级 from t_student group by 斑级) as b where t_student.荣誉分=b.最高荣誉分 and t_student.斑级=b.斑级--测试结果
学号 学生姓名 荣誉分 斑级
----------- -------- ----------- ----
4 赵六 90 002
3 王五 100 001(所影响的行数为 2 行)