免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2958 | 回复: 8
打印 上一主题 下一主题

该语句怎么用sql server 实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-08 09:26 |只看该作者 |倒序浏览
如何查询某个字段的80%的记录
create table a
(class varchar2(,
student varchar2(100),
score number(3)
)
class 代表班,应该有一班,二班,三班...
student学生
score分数

有多个班,每个班有多名同学


现在想查询并显示每个班分数前的15%名的同学名单,请问该如何写查询语句

论坛徽章:
0
2 [报告]
发表于 2005-11-08 09:56 |只看该作者
用select top count()*0.15,group by 班级,order

论坛徽章:
0
3 [报告]
发表于 2005-11-08 10:05 |只看该作者

应该没有这么简单吧

我要把所有的记录信息都显示出来,而不是一个count(*)信息,还要分班显示,每个班的15%的人信息

论坛徽章:
0
4 [报告]
发表于 2005-11-08 10:06 |只看该作者
在oracle里面 使用rank()over()来实现的,就是不知道sql server 里面怎么实现

论坛徽章:
0
5 [报告]
发表于 2005-11-08 11:21 |只看该作者
是的,想了一下,好像不对,不过换一种思路,先取得班级数,然后根据班级数select top15倒入一张临时表就可以解决了

论坛徽章:
0
6 [报告]
发表于 2005-11-08 12:59 |只看该作者
declare @t table
(class varchar(,
student varchar(100),
scorce int
)

insert into @t values(\'一班\',\'A1\',\'50\')
insert into @t values(\'一班\',\'A2\',\'60\')
insert into @t values(\'一班\',\'A3\',\'70\')
insert into @t values(\'一班\',\'A4\',\'80\')
insert into @t values(\'一班\',\'A5\',\'90\')


insert into @t values(\'二班\',\'B1\',\'60\')
insert into @t values(\'二班\',\'B2\',\'70\')
insert into @t values(\'二班\',\'B3\',\'50\')
insert into @t values(\'二班\',\'B4\',\'80\')
insert into @t values(\'二班\',\'B5\',\'90\')


insert into @t values(\'三班\',\'C1\',\'50\')
insert into @t values(\'三班\',\'C2\',\'60\')
insert into @t values(\'三班\',\'C4\',\'70\')
insert into @t values(\'三班\',\'C3\',\'80\')
insert into @t values(\'三班\',\'C5\',\'90\')
insert into @t values(\'三班\',\'C6\',\'100\')
insert into @t values(\'三班\',\'C7\',\'60\')
insert into @t values(\'三班\',\'C8\',\'70\')
insert into @t values(\'三班\',\'C9\',\'80\')
insert into @t values(\'三班\',\'C10\',\'90\')


select top 4 sum(scorce)as scorce,class, student from @t a where class = \'一班\'  group by a.class, a.student
union
select top 4  sum(scorce)as scorce,class, student from @t a where class = \'二班\'   group by a.class, a.student
union
select top 4  sum(scorce)as scorce,class, student from @t a where class = \'三班\' group by a.class, a.student
order by a.scorce desc

论坛徽章:
0
7 [报告]
发表于 2005-11-08 14:26 |只看该作者
如果我有100个班,是不是一定要写99个union呢,而且,还是hardcode的,如果班级数不定呢?

论坛徽章:
0
8 [报告]
发表于 2005-11-08 14:43 |只看该作者
SELECT *
FROM aa a
WHERE EXISTS
          (SELECT 1
         FROM ((SELECT TOP 40 percent b.student
                 FROM aa b
                 WHERE a.class = b.class
                 order by b.scorce desc
                 
                )) b
         WHERE b.student = a.student)

记录太少
取每个班的前40%,取出来后不分前后
结果是这样的

一班        A4        80
一班        A5        90
二班        B4        80
二班        B5        90
三班        C5        90
三班        C6        100
三班        C9        80
三班        C10        90

论坛徽章:
0
9 [报告]
发表于 2005-11-08 16:15 |只看该作者
应该采取动态变量吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP