如何只取分组的第N行?

startexcel 2007-02-02 03:36:58
表是id,name,price
我要按name分组,然后取每个name的price>100的前50行显示


...全文
410 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pumawang 2007-03-14
  • 打赏
  • 举报
回复
select sbjID,score From ASSIGNMENT

SBJID SCORE
-------- --------
1 32
1 3
1 89
1 23
2 78
2 78
2 79
2 80
2 83
1 85
2 86
1 97

12 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 46/ms]

[Executed: 07-3-14 上午12时01分38秒 ] [Execution: 63/ms]


select sbjID,score ,rk From (
select sbjID,score, rank() OVER (PARTITION BY SBJID ORDER BY SCORE desc) rk From ASSIGNMENT where score >60) tt
where rk <=5

SBJID SCORE RK
-------- -------- -----
1 97 1
1 89 2
1 85 3
2 86 1
2 83 2
2 80 3
2 79 4
2 78 5
2 78 5

9 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 47/ms]

[Executed: 07-3-14 上午12时01分43秒 ] [Execution: 47/ms]


做了试验,结果应该大概是楼主想要的,
如果一定对这个 50 要求比较严格的话,
可以考虑在 ORDER BY SCORE .... 这个地方多加些字段,最好是唯一的,
不然并列的可能就一起都显示了。


希望楼主试一试 。。。
pumawang 2007-03-13
  • 打赏
  • 举报
回复
select Name,Price,rk
From (select Name,price, rank() OVER (PARTITION BY NameORDER BY pricedesc) rk From tab
where price >100 ) tt
where rk <=50

pumawang 2007-03-13
  • 打赏
  • 举报
回复
大树你的不对吧,
这样的结果只是 大于 100 的 所有Name分组数据的前 50条数据而已。
不是每个Name分组的前50位。

lbl310 2007-02-12
  • 打赏
  • 举报
回复
select name, price from price
group by name, price
having price > 100
order by price desc
fetch first 50 rows only;

这样就行
lbl310 2007-02-12
  • 打赏
  • 举报
回复
select name, price from price
group by name
having price > 500
order by price desc
fetch first 50 rows only;
liaoqianlin 2007-02-08
  • 打赏
  • 举报
回复
select name, sum(price) from price
group by name
having sum(price) > 500
order by sum(price) desc
fetch first 50 rows only
mouseshuai 2007-02-06
  • 打赏
  • 举报
回复
不好意思,看错了
mouseshuai 2007-02-06
  • 打赏
  • 举报
回复
楼上的,没有按name分组啊
bflovesnow 2007-02-06
  • 打赏
  • 举报
回复
select * from table t where id in
(select id from table t2 where price > 100 and t.name = t2.name fetch first 50 rows only)

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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