查出每个班级的总分前两名的学生
生信息表:students 学生编号    学生姓名     学生所班级id     name           class001     学生一        class_01

002         学生二        class_01003         学生三        class_01004         学生四        class_02005         学生五        class_02006         学生六        class_02 
成绩表:results学生编号    科目名称        分数 id         course_name      course_score001          语文            80
002          语文            80
003          语文            83
004          语文            80
005          语文            80
006          语文            60
001          数学            80
002          数学            80
003          数学            60
004          数学            80
005          数学            80
006          数学            82
001          外语            80
002          外语            80
003          外语            60
004          外语            80
005          外语            80
006          外语            75
怎么用sql server 查出每个班级的总分前两名的学生?

解决方案 »

  1.   

    --按某一字段分组取最大(小)值所在行的数据(2007-10-23于浙江杭州)
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
      

  2.   

    create table students(id varchar(10),cname varchar(10),class varchar(10))
    insert into students values('001','学生一','class_01') 
    insert into students values('002','学生二','class_01') 
    insert into students values('003','学生三','class_01') 
    insert into students values('004','学生四','class_02') 
    insert into students values('005','学生五','class_02') 
    insert into students values('006','学生六','class_02')   
    create table results(id varchar(10),course_name varchar(10),course_score int)
    insert into results values('001','语文',80) 
    insert into results values('002','语文',80) 
    insert into results values('003','语文',83) 
    insert into results values('004','语文',80) 
    insert into results values('005','语文',80) 
    insert into results values('006','语文',60) 
    insert into results values('001','数学',80) 
    insert into results values('002','数学',80) 
    insert into results values('003','数学',60) 
    insert into results values('004','数学',80) 
    insert into results values('005','数学',80) 
    insert into results values('006','数学',82) 
    insert into results values('001','外语',80) 
    insert into results values('002','外语',80) 
    insert into results values('003','外语',60) 
    insert into results values('004','外语',80) 
    insert into results values('005','外语',80) 
    insert into results values('006','外语',75) 
    goselect m.* from (select a.class , a.id , course_score = sum(b.course_score) from students a,results b where a.id = b.id group by a.class , a.id) m 
    where 2 > (select count(*) from (select a.class , a.id , course_score = sum(b.course_score) from students a,results b where a.id = b.id group by a.class , a.id) n where n.class = m.class and n.course_score > m.course_score ) 
    order by m.class , m.course_score desc , m.iddrop table students,results/*
    class      id         course_score 
    ---------- ---------- ------------ 
    class_01   001        240
    class_01   002        240
    class_02   004        240
    class_02   005        240(所影响的行数为 4 行)
    */
      

  3.   

    如果你实际的数据库和我上面一样,只需要拷贝中间的一段SQL即可.
    select m.* from (select a.class , a.id , course_score = sum(b.course_score) from students a,results b where a.id = b.id group by a.class , a.id) m 
    where 2 > (select count(*) from (select a.class , a.id , course_score = sum(b.course_score) from students a,results b where a.id = b.id group by a.class , a.id) n where n.class = m.class and n.course_score > m.course_score ) 
    order by m.class , m.course_score desc , m.id
      

  4.   


    借潇洒老乌龟数据一用
    create table students(id varchar(10),cname varchar(10),class varchar(10))
    insert into students values('001','学生一','class_01') 
    insert into students values('002','学生二','class_01') 
    insert into students values('003','学生三','class_01') 
    insert into students values('004','学生四','class_02') 
    insert into students values('005','学生五','class_02') 
    insert into students values('006','学生六','class_02')   
    create table results(id varchar(10),course_name varchar(10),course_score int)
    insert into results values('001','语文',80) 
    insert into results values('002','语文',80) 
    insert into results values('003','语文',83) 
    insert into results values('004','语文',80) 
    insert into results values('005','语文',80) 
    insert into results values('006','语文',60) 
    insert into results values('001','数学',80) 
    insert into results values('002','数学',80) 
    insert into results values('003','数学',60) 
    insert into results values('004','数学',80) 
    insert into results values('005','数学',80) 
    insert into results values('006','数学',82) 
    insert into results values('001','外语',80) 
    insert into results values('002','外语',80) 
    insert into results values('003','外语',60) 
    insert into results values('004','外语',80) 
    insert into results values('005','外语',80) 
    insert into results values('006','外语',75) select  top 4
    a.class,b.id,sum(b.course_score)as score
    from students a left join results b
    on a.id=b.id
    group by  a.class,b.id
    order by  sum(b.course_score) desc,a.class/*
    class      id         score       
    ---------- ---------- ----------- 
    class_01   002        240
    class_01   001        240
    class_02   005        240
    class_02   004        240(所影响的行数为 4 行)
    */
      

  5.   

    九天兄,如果有三个班级,你还得改为select top 6?此法不可取.