SQL分组查询问题(获取每个班前三名)

Hajoio 2009-03-17 12:59:31
例如这样一个表tb
id|class|marks
1|1|100
2|1|99
3|2|97
4|3|85
5|2|90
....
还有很多。
我想在一次查询当中,获取每个班的前三名。怎么实现????????
...全文
2010 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hajoio 2009-03-17
  • 打赏
  • 举报
回复
1楼的速度很快,而且是正确的!不过本人是mysql有些问题。
2楼的代码自己没有看懂,但是,在mysql下是正确的~~~~~
后面呢…………没有细细看了!基础不行!!!
分少,大家多多包涵!
Hajoio 2009-03-17
  • 打赏
  • 举报
回复
大家都好积极啊~~哈哈~~~
不过,这些好像是mssql才行的~~
那个mysql如何实现呢?
dawugui 2009-03-17
  • 打赏
  • 举报
回复
--sql 2005
select id,class,marks from
(
select t.* , px = row_number() rank(partition class order by marks desc) from tb t
) m
where px <= 3
dawugui 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 Hajoio 的帖子:]
例如这样一个表tb
id|class|marks
1|1|100
2|1|99
3|2|97
4|3|85
5|2|90
....
还有很多。
我想在一次查询当中,获取每个班的前三名。怎么实现????????
[/Quote]

select t.* from tb t where marks in (select top 3 marks from tb where class = t.class order by marks desc)
ws_hgo 2009-03-17
  • 打赏
  • 举报
回复
人齐结贴
ws_hgo 2009-03-17
  • 打赏
  • 举报
回复
if object_id('tb') is not null drop table tb
create table tb(id int,class int,marks int)

insert tb
select 1,1,100 union all
select 2,1,99 union all
select 3,1,96 union all
select 4,1,90 union all
select 5,2,97 union all
select 6,2,85 union all
select 7,2,93 union all
select 8,2,85 union all
select 9,3,90
select bb.* from (select *,row_number() over (partition by class order by marks) rank from tb) bb
where bb.rank<=3 order by bb.class,bb.rank

id class marks rank
----------- ----------- ----------- --------------------
4 1 90 1
3 1 96 2
2 1 99 3
6 2 85 1
8 2 85 2
7 2 93 3
9 3 90 1

(7 行受影响)
中国风 2009-03-17
  • 打赏
  • 举报
回复
--用dense_rank
select *
from (select *,dense_RANK() OVER(PARTITION BY class ORDER BY marks desc) as [Order] FROM tb) a
where [Order]<=3
sunriseknits 2009-03-17
  • 打赏
  • 举报
回复
后面几个还需要加上order by class,marks desc
ai_li7758521 2009-03-17
  • 打赏
  • 举报
回复
--SQL2005适用
if object_id('tb') is not null drop table tb
go
create table tb(id int,class int,marks int)

insert tb
select 1,1,100 union all
select 2,1,99 union all
select 3,1,96 union all
select 4,1,90 union all
select 5,2,97 union all
select 6,2,85 union all
select 7,2,93 union all
select 8,2,85 union all
select 9,3,90
--如果并列都算用RANK(),排除并列用DENSE_RANK()
select *
from (select *,RANK() OVER(PARTITION BY class ORDER BY marks desc) as [Order] FROM tb) a
where a.[Order] between 1 and 3
zzxap 2009-03-17
  • 打赏
  • 举报
回复
select t.* from tb t where id in(select top 3 id from tb where class=t.class order by marks desc) order by class,marks desc

水族杰纶 2009-03-17
  • 打赏
  • 举报
回复
SELECT * FROM TB T WHERE CHECKSUM(*) IN (SELECT TOP 3 CHECKSUM(*) FROM TB WHERE CLASS=T.CLASS ORDER BY MARKS DESC)
jwdream2008 2009-03-17
  • 打赏
  • 举报
回复
select * from (select *,row_number() over(partition by class order by marks desc)as [order] from tb) where [order]<4
百年树人 2009-03-17
  • 打赏
  • 举报
回复
select *
from tb t
where (select count(1)+1 from tb where class=t.class and marks>t.marks)<=3
子陌红尘 2009-03-17
  • 打赏
  • 举报
回复
select t.* from tb t where id in(select top 3 id from tb where class=t.class order by marks desc) order by class,marks desc

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧