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

解决方案 »

  1.   

    SELECT * FROM t_student A WHERE 荣誉分 = (SELECT MAX(荣誉分) FROM t_student WHERE 斑级=A.斑级 ORDER BY 荣誉分 DESC)
      

  2.   

    SELECT * FROM t_student A WHERE 荣誉分 = (SELECT TOP 1 荣誉分 FROM t_student WHERE 斑级=A.斑级 ORDER BY 荣誉分 DESC)
      

  3.   

    select top 2(荣辱分) from table_name where group by 班级 desc having 荣誉分
      

  4.   

    select 学号,学生姓名,max(荣誉分) as 荣誉分,斑级 from tb 
    group by 学号,学生姓名,斑级 
      

  5.   

    SELECT 班级,MAX(荣誉分)INTO #J FORM t_student GROUP BY 班级
    SELECT * FROM  t_student INNER JOIN #J ON t_student.荣誉分=#J.荣誉分 AND t_student.班级=#J.班级
      

  6.   

    可以有两种思路:
    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.班级都经过测试,可以实现
      

  7.   

    还可以参考以下的语句
    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
      

  8.   

    select a.* 
    from t_student a,
    (select 斑级,max(荣誉分) as 荣誉分 from t_student group by 斑级) b
    where a.斑级=b.斑级 and a.荣誉分=b.荣誉分
      

  9.   

    create table t_student (学号[int] IDENTITY (1, 1) NOT NULL ,学生姓名 varchar (10),荣誉分 [decimal](12, 2) NULL , 斑级 char (3))insert into t_student
    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
      

  10.   

    上面建表有点问题改一下create table t_student (学号 [int],学生姓名 varchar (10),荣誉分 [decimal](12, 2) NULL , 斑级 char (3))
    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
      

  11.   

    又是类似的分组求最大的问题select a.*
    from t_student a,(select 班级,max(荣誉分) from t_student group by 班级)b
    where a.班级=b.班级 and a.荣誉分=b.荣誉分
    order by 班级
      

  12.   

    select * from cj right join
    (select ryf2=max(ryf) from cj group by class)a
    on cj.ryf=a.ryf2
      

  13.   

    create table cj(num int,xm varchar(10),ryf int,class varchar(10))
    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
      

  14.   

    --测试数据
    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 行)
      

  15.   

    刚才打错了,改改数据库名
    --测试数据
    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 行)