100分求语法详解

一品梅 2008-05-09 01:11:08
set nocount on
create table test(S# varchar(20),SNAME varchar(20),GRADE varchar(20),TIME varchar(20))
insert into test select '1','张三','90','2008-05-04'
insert into test select '1','张三','70','2007-09-01'
insert into test select '1','张三','50','2006-02-01'
insert into test select '1','张三','56','2004-07-15'
insert into test select '1','张三','70','2008-04-06'
insert into test select '2','李四','60','2005-02-13'
insert into test select '2','李四','30','2008-04-10'
insert into test select '2','李四','80','2008-01-18'
go
--测试
select * from (select *,
row_number() over(partition by S# order by GRADE desc) as rowindex
from test) as a
where rowindex<=(select count(*) from test where S#=a.S#)*0.5


--删除测试环境
drop table test
set nocount off

/*
1 张三 90 2008-05-04 1
1 张三 70 2007-09-01 2
2 李四 80 2008-01-18 1*/

这怎么和我学的T-SQL语句不一样啊,读不懂,是2005新增的语法么?求详细解释,高分送上。
...全文
106 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Limpire 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 jinjazz 的回复:]
引用 15 楼 Limpire 的回复:
引用 3 楼 only_endure 的回复:
能把这段语句解释一下吗?同样的再用2000翻译一下。谢谢了。

2000/2005用这个都可以了

SQL codeselect * from Test as t where TIME in (select top 10 percent TIME from Test where S#=t.S# order by GRADE desc, TIME desc)


那是因为只需要查询是否存在,而没有要求出具体的排名
[/Quote]

you are right
一品梅 2008-05-09
  • 打赏
  • 举报
回复
我再仔细想想,结帖,谢谢了。
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 Limpire 的回复:]
引用 3 楼 only_endure 的回复:
能把这段语句解释一下吗?同样的再用2000翻译一下。谢谢了。

2000/2005用这个都可以了

SQL codeselect * from Test as t where TIME in (select top 10 percent TIME from Test where S#=t.S# order by GRADE desc, TIME desc)
[/Quote]

那是因为只需要查询是否存在,而没有要求出具体的排名
Limpire 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 only_endure 的回复:]
能把这段语句解释一下吗?同样的再用2000翻译一下。谢谢了。
[/Quote]
2000/2005用这个都可以了
select * from Test as t where TIME in (select top 10 percent TIME from Test where S#=t.S# order by GRADE desc, TIME desc)
areswang 2008-05-09
  • 打赏
  • 举报
回复
跟排名差不多
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
row_number函数早在sql2005出来之前就已经被其他数据库语法支持了,比如oracle,db2,这个是微软弱智才这么晚出来
pt1314917 2008-05-09
  • 打赏
  • 举报
回复
ROW_NUMBER
返回结果集分区内行的序列号,每个分区的第一行从 1 开始。

语法:
ROW_NUMBER ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> )

备注:
ORDER BY 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。

参数:
<partition_by_clause>
将 FROM 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。
<order_by_clause>
确定将 ROW_NUMBER 值分配给分区中的行的顺序。
一品梅 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jinjazz 的回复:]
这个语句看起来好眼熟 -_- ¦ ¦ ¦
[/Quote]
是引用大师的回帖,不好意思,呵呵。
wzy_love_sly 2008-05-09
  • 打赏
  • 举报
回复
(select count(*) from test where S#=a.S#)*0.5

1是5个 5*0.5=2
2是3个 3*0.5=1

结果
1是2个
2是1个
Limpire 2008-05-09
  • 打赏
  • 举报
回复
row_number()
行号

partition by S#
按S#分组

order by GRADE desc
按GRADE排序
jinjazz 2008-05-09
  • 打赏
  • 举报
回复
这个语句看起来好眼熟 -_-|||
一品梅 2008-05-09
  • 打赏
  • 举报
回复
row_number() over(partition ...
areswang 2008-05-09
  • 打赏
  • 举报
回复
2005地
wzy_love_sly 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 only_endure 的回复:]
能把这段语句解释一下吗?同样的再用2000翻译一下。谢谢了。
[/Quote]

2000没有row_number(),临时表identity(int,1,1)代替
wzy_love_sly 2008-05-09
  • 打赏
  • 举报
回复
哪不懂only妞?
一品梅 2008-05-09
  • 打赏
  • 举报
回复
能把这段语句解释一下吗?同样的再用2000翻译一下。谢谢了。
一品梅 2008-05-09
  • 打赏
  • 举报
回复
忘了说题目了:
问题:按S#分组后,按成绩降序排列取每个同学TOP N
N=该同学的考试次数*10%
chuifengde 2008-05-09
  • 打赏
  • 举报
回复
2005的

34,591

社区成员

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

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