日期:2014-05-19  浏览次数:20527 次

关于一个查询语句,求助
现在有个表A,包括学生姓名name,3门功课成绩b1,b2,b3,平均成绩c,学生类别type,现在需要一个查询语句查询出按类别和平均成绩的排序,并多出一列1,2,3。。。
比如有6条记录:
name     b1     b2     b3     c     type
张三     90     90     90     90     1
李四     99     99     99     99     1
王五     80     80     80     80     2
嘿嘿     70     70     70     70     1
哈哈     50     50     50     50     2
呵呵     60     60     60     60     2

查询后要得出:
name     b1     b2     b3     c     type     排名
李四     99     99     99     99     1           1        
张三     90     90     90     90     1           2    
嘿嘿     70     70     70     70     1           3
王五     80     80     80     80     2           1
呵呵     60     60     60     60     2           2
哈哈     50     50     50     50     2           3

这个语句怎么写,谢谢了!


------解决方案--------------------
select name,b1,b2,b3,c,type,identity(int,1,1) as paimingc into #t from A order by c desc;
select * from #t;
drop table #t;
------解决方案--------------------
if object_id( 'pubs..tb ') is not null
drop table tb
go

create table tb(name varchar(10),b1 int,b2 int,b3 int,c int,type int)
insert into tb(name,b1,b2,b3,c,type) values( '张三 ', 90, 90, 90, 90, 1)
insert into tb(name,b1,b2,b3,c,type) values( '李四 ', 99, 99, 99, 99, 1)
insert into tb(name,b1,b2,b3,c,type) values( '王五 ', 80, 80, 80, 80, 2)
insert into tb(name,b1,b2,b3,c,type) values( '嘿嘿 ', 70, 70, 70, 70, 1)
insert into tb(name,b1,b2,b3,c,type) values( '哈哈 ', 50, 50, 50, 50, 2)
insert into tb(name,b1,b2,b3,c,type) values( '呵呵 ', 60, 60, 60, 60, 2)

select * , 排名=(select count(1) from tb where type=a.type and c> a.c)+1 from tb a order by type ,排名

drop table tb

/*
name b1 b2 b3 c type 排名
---------- ----------- ----------- ----------- ----------- ----------- -----------
李四 99 99 99 99 1 1
张三 90 90 90 90 1 2
嘿嘿 70 70 70 70 1 3
王五 80 80 80 80 2 1
呵呵 60 60 60 60 2 2
哈哈 50 50 50 50 2 3

(所影响的行数为 6 行)
*/
------解决方案--------------------
drop table A
go
create table A(name varchar(10),b1 int,b2 int,b3 int,c int,type int)
insert into A
select '张三 ',90,90,90,90,1
union select &