来看看这个SQl怎么写,有关分组取前N条的

carolbaby 2003-12-08 07:09:02
一个表中,希望按照groupid分组后,再在组中按照score排序,取出每组的前50%的记录(不一定是50%,可能会是任何一个比例。)

create table testTable
(
groupid int,
score int,
title varchar(64)
)


insert into testtable values(1,80, 'title1')
insert into testtable values(1,90, 'title2')
insert into testtable values(1,67, 'title3')
insert into testtable values(1,93, 'title4')
insert into testtable values(1,84, 'title5')
insert into testtable values(2,80, 'title6')
insert into testtable values(2,40, 'title7')
insert into testtable values(2,60, 'title8')
insert into testtable values(2,50, 'title9')
insert into testtable values(2,59, 'title10')
insert into testtable values(2,45, 'title11')
insert into testtable values(2,65, 'title12')

希望得到:
1 90 title2
1 93 title4
1 84 title5
2 80 title6
2 60 title8
2 59 title10
2 65 title12
...全文
97 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
carolbaby 2003-12-10
  • 打赏
  • 举报
回复
真好用,谢谢邹建!
dlpseeyou 2003-12-09
  • 打赏
  • 举报
回复
回答的真好!学习
pengdali 2003-12-09
  • 打赏
  • 举报
回复
使用 TOP 和 PERCENT 限制结果集
TOP 子句限制返回到结果集中的行数。

TOP n [PERCENT]

n 指定返回的行数。如果未指定 PERCENT,n 就是返回的行数。如果指定了 PERCENT,n 就是返回的结果集行的百分比,如下所示:

TOP 120 /*Return the top 120 rows of the result set. */
TOP 15 PERCENT /* Return the top 15% of the result set. */.

如果一个 SELECT 语句既包含 TOP 又包含 ORDER BY 子句,那么返回的行将会从排序后的结果集中选择。整个结果集按照指定的顺序建立并且返回排好序的结果集的前 n 行。

限制结果集大小的另一种方法是在执行一个语句之前执行 SET ROWCOUNT n 语句。SET ROWCOUNT 与 TOP 的不同之处在于:

SET ROWCOUNT 限制适用对 ORDER BY 取值后在结果集中生成行。如果指定了 ORDER BY,SELECT 语句将在从某个已根据指定的 ORDER BY 分类进行了排序的值集中选择 n 行后终止。


TOP 子句适用于指定了该子句的单个 SELECT 语句。在执行另一个 SET ROWCOUNT 语句之前,SET ROWCOUNT 会一直有效,例如执行 SET ROWCOUNT 0 将会关闭此选项。
pengdali 2003-12-09
  • 打赏
  • 举报
回复
select * from testtable a where score in (select top 50 percent score from testtable where groupid=a.groupid order by score desc)
pengdali 2003-12-09
  • 打赏
  • 举报
回复
select * from testtable a where score in (select top 50 percent score from testtable where groupid=a.groupid)
victorycyz 2003-12-09
  • 打赏
  • 举报
回复
你给出的结果集并没有按照score排序呀。
wzh1215 2003-12-08
  • 打赏
  • 举报
回复
如果title为主键的话:

select * from testTable a
where title in(select top 50 percent title from testTable where groupid=a.groupid order by score desc)
zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--下面是数据测试

--测试数据,我直接在原表上加了id字段.所以不用临时表
create table testTable(
id int identity(1,1),
groupid int,
score int,
title varchar(64))
insert into testtable values(1,80, 'title1')
insert into testtable values(1,90, 'title2')
insert into testtable values(1,67, 'title3')
insert into testtable values(1,93, 'title4')
insert into testtable values(1,84, 'title5')
insert into testtable values(2,80, 'title6')
insert into testtable values(2,40, 'title7')
insert into testtable values(2,60, 'title8')
insert into testtable values(2,50, 'title9')
insert into testtable values(2,59, 'title10')
insert into testtable values(2,45, 'title11')
insert into testtable values(2,65, 'title12')

--查询
select * from testTable a
where id in(select top 50 percent id from testTable where groupid=a.groupid order by score desc)

go
--删除测试环境
drop table testtable

/*--测试结果

id groupid score title
----------- ----------- ----------- ---------
2 1 90 title2
4 1 93 title4
5 1 84 title5
6 2 80 title6
8 2 60 title8
10 2 59 title10
12 2 65 title12

(所影响的行数为 7 行)

--*/
zjcxc 元老 2003-12-08
  • 打赏
  • 举报
回复
--需要用临时表来解决
select id=identity(int,1,1),* into #t from testTable

--查询
select * from #t a
where id in(select top 50 percent id from #t where groupid=a.groupid order by score desc)

--删除临时表
dorp table #t
zhoudongchao 2003-12-08
  • 打赏
  • 举报
回复
衰,看出可以通过第三个字段组合来去,建议搞个存储过程搞定

34,838

社区成员

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

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