求一统计表(高分回报)
有如下一张表:
排名 stuname stuid classid 总分
4 张三 101 1 504.0
6 李四 102 1 485.0
2 王五 103 1 530.0
1 小白 104 1 544.0
3 宋华 105 1 523.0
5 钱一 106 1 486.0
1 赵善 201 2 558.0
2 陈锋 202 2 521.0
对这张表做查询,使得得到的结果为:(就是要去除班级的前十名单和年级的前十名单)
(班级是动态的哈)
名次 1班 2班 年级
第一名 小白(544.0) 赵善(558.0) 赵善(2班)
第二名 王五(530.0) 陈锋(521.0) 小白(1班)
谢谢给为老大们先
最近搞一关于这方面得咚咚,把我快搞挂了
代码共享,谢谢啦
问题点数:100、回复次数:9Top
1 楼splory(爽儿)回复于 2006-02-25 15:37:35 得分 0
正确的:
对这张表做查询,使得得到的结果为:(就是求班级的前十名单和年级的前十名单)
(班级是动态的哈)
Top
2 楼splory(爽儿)回复于 2006-02-25 15:38:00 得分 0
写错了Top
3 楼splory(爽儿)回复于 2006-02-25 17:25:23 得分 0
高手快来给个帮手阿
谢谢啦Top
4 楼rouqu(石林#黄果树)回复于 2006-02-25 18:18:06 得分 0
我自己最差的就是写复杂的SQL。。。Top
5 楼rouqu(石林#黄果树)回复于 2006-02-25 18:21:48 得分 0
你原始表如果有很多个班级怎么办?Top
6 楼wgsasd311(自强不息)回复于 2006-02-25 18:47:25 得分 0
select 名次,
[1班]=max(case flag when 1 then stuname+'('+cast(总分 as varchar)+')' else '' end ),
[2班]=max(case flag when 2 then stuname+'('+cast(总分 as varchar)+')' else '' end ),
[年级]=max(case flag when 0 then stuname+'('+cast(总分 as varchar)+')' else '' end ) from
(select top 10 名次=(select count(1) from tb where 总分>=a.总分 ),stuname,总分,0 as flag
from tb a
union all
select top 10 名次=(select count(1) from tb where 总分>=a.总分 and classid=a.classid ),stuname,总分,1
from tb a where classid=1
union all
select top 10 名次=(select count(1) from tb where 总分>=a.总分 and classid=a.classid ),stuname,总分,2
from tb a where classid=2)aa
group by 名次 order by 名次Top
7 楼wgsasd311(自强不息)回复于 2006-02-25 19:21:17 得分 100
create table tb(排名 int,stuname varchar(10),stuid int,classid int ,总分 decimal(9,2))
insert into tb
select 4,'张三',101,1,504.0 union all
select 6,'李四',102,1,485.0 union all
select 2,'王五',103,1,530.0 union all
select 1,'小白',104,1,544.0 union all
select 3,'宋华',105,1,523.0 union all
select 5,'钱一',106,1,486.0 union all
select 1,'赵善',201,2,558.0 union all
select 2,'陈锋',202,2,521.0
go
declare @s varchar(8000),@s2 varchar(8000)
set @s='select a.*,b.年级 from (select top 10 [名次]=排名'
select @s=@s+',['+cast(classid as varchar)+'班]=max(case classid when '+cast(classid as varchar)
+ ' then stuname + ''(''+cast(总分 as varchar)+'')'' else '''' end)'
from tb group by classid
--print @s
set @s=@s+' from tb group by 排名 )a '
set @s2=' left join (select top 10 名次=(select count(1) from tb where 总分>=a.总分 ),
年级=stuname+''(''+cast(总分 as varchar)+'')'' from tb a ) b on a.名次=b.名次 order by a.名次'
exec(@s+@s2)
go
drop table tb
goTop
8 楼wgsasd311(自强不息)回复于 2006-02-25 19:22:32 得分 0
create table tb(排名 int,stuname varchar(10),stuid int,classid int ,总分 decimal(9,2))
insert into tb
select 4,'张三',101,1,504.0 union all
select 6,'李四',102,1,485.0 union all
select 2,'王五',103,1,530.0 union all
select 1,'小白',104,1,544.0 union all
select 3,'宋华',105,1,523.0 union all
select 5,'钱一',106,1,486.0 union all
select 1,'赵善',201,2,558.0 union all
select 2,'陈锋',202,2,521.0
go
select 名次,
[1班]=max(case flag when 1 then stuname+'('+cast(总分 as varchar)+')' else '' end ),
[2班]=max(case flag when 2 then stuname+'('+cast(总分 as varchar)+')' else '' end ),
[年级]=max(case flag when 0 then stuname+'('+cast(总分 as varchar)+')' else '' end ) from
(select top 10 名次=(select count(1) from tb where 总分>=a.总分 ),stuname,总分,0 as flag
from tb a
union all
select top 10 名次=(select count(1) from tb where 总分>=a.总分 and classid=a.classid ),stuname,总分,1
from tb a where classid=1
union all
select top 10 名次=(select count(1) from tb where 总分>=a.总分 and classid=a.classid ),stuname,总分,2
from tb a where classid=2)aa
group by 名次 order by 名次
go
drop table tb
go
Top
9 楼zjcxc(邹建)回复于 2006-02-25 20:37:31 得分 0
-- sql 2005中的处理方法
create table tb(排名 int,stuname varchar(10),stuid int,classid int ,总分 decimal(9,2))
insert into tb
select 4,'张三',101,1,504.0 union all
select 6,'李四',102,1,485.0 union all
select 2,'王五',103,1,530.0 union all
select 1,'小白',104,1,544.0 union all
select 3,'宋华',105,1,523.0 union all
select 5,'钱一',106,1,486.0 union all
select 1,'赵善',201,2,558.0 union all
select 2,'陈锋',202,2,521.0
go
declare @s varchar(max)
set @s=''
select @s=@s+','+quotename(classid)
from tb
group by classid
order by classid
set @s=stuff(@s,1,1,'')
exec('
select a.*,b.stuname
from(
select *
from(
select
stuname = stuname + quotename(总分, ''()''), classid,
排名
from tb
where 排名<=10
)data
pivot(
max(stuname)
for classid in('+@s+')
)p
)a,(
select top 10 stuname, 排名
from(
select
stuname = stuname + quotename(classid, ''()''), classid,
排名=row_number() over(order by 总分 desc)
from tb
)data
)b where a.排名=b.排名')
go
drop table tb
-- 结果
排名 1 2 stuname
----------- ------------ ------------ -----------
1 小白(544.00) 赵善(558.00) 赵善(2)
2 王五(530.00) 陈锋(521.00) 小白(1)
3 宋华(523.00) NULL 王五(1)
4 张三(504.00) NULL 宋华(1)
5 钱一(486.00) NULL 陈锋(2)
6 李四(485.00) NULL 张三(1)
(6 row(s) affected)
Top




