sql server 2000 对应 sql server 2005的row_number()、rank()、DENSE_RANK( )、ntile( )等用法

转自CSDN:http://blog.csdn.net/htl258/article/details/4006717

SQL server 2005新增的几个函数,分别是row_number( )、rank( )、,DENSE_RANK( )、ntile( )下面以实例分别简单讲解。

1.row_number( )
         先来点数据,先建个表

SET NOCOUNT ON
CREATE TABLE Person(
FirstName VARCHAR(10),
Age INT,
Gender CHAR(1))
INSERT INTO Person VALUES (‘Ted‘,23,‘M‘)
INSERT INTO Person VALUES (‘John‘,40,‘M‘)
INSERT INTO Person VALUES (‘George‘,6,‘M‘)
INSERT INTO Person VALUES (‘Mary‘,11,‘F‘)
INSERT INTO Person VALUES (‘Sam‘,17,‘M‘)
INSERT INTO Person VALUES (‘Doris‘,6,‘F‘)
INSERT INTO Person VALUES (‘Frank‘,38,‘M‘)
INSERT INTO Person VALUES (‘Larry‘,5,‘M‘)
INSERT INTO Person VALUES (‘Sue‘,29,‘F‘)
INSERT INTO Person VALUES (‘Sherry‘,11,‘F‘)
INSERT INTO Person VALUES (‘Marty‘,23,‘F‘)

直接用例子说明问题:

SELECT ROW_NUMBER() OVER (ORDER BY Age) AS [Row Number by Age],
FirstName,
Age
FROM Person

出现的数据如下
Row Number by Age                FirstName            Age
--------------------------                 ----------            --------
1                                                Larry                   5
2                                                Doris                   6
3                                                George               6
4                                                Mary                   11
5                                                Sherry                 11
6                                                Sam                    17
7                                                Ted                     23
8                                                Marty                   23
9                                                Sue                     29
10                                              Frank                  38
11                                              John                    40

可以观察到,是根据年龄升序排列了,并且row_number()是给出了序列号了,这个序列号被重命名为Row Number by Age,
sql server2000对比:如果在sql server2000中实现相对麻烦一些,我们可以利用IDENTITY()函数实现,但IDENTITY()函数只能用在sql server2000临时表中,因此需要将数据检索到临时表里。select identity(int,1,1) as [Row Number by Age],FirstName,Age into #A from Person order by Ageselect * from #Adrop table #a
如果不想按年龄排序,可以这样写SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS [Row Number by Record Set],
FirstName,
Age
FROM Person
另外一个例子SELECT ROW_NUMBER() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender],
FirstName,
Age,
Gender
FROM Person

这里是按性别划分区间了,同一性别再按年龄来排序,输出结果如下

Partition by Gender         FirstName         Age                Gender
-------------------- ---------- ----------- ------
1                           Doris             6                  F
2                           Mary              11                 F
3                           Sherry            11                 F
4                           Sue               29                 F
1                           Larry             5                  M
2                           George            6                  M
3                           Sam               17                 M
4                           Ted               23                 M
5                           Marty             23                 M
6                           Frank             38                 M
7                           John              40                 M

注意,姓名M开始,序号又从1,2,3开始了

 
2.RANK( )函数         先看例子SELECT RANK() OVER (ORDER BY Age) AS [Rank by Age],
FirstName,
Age
FROM Person
输出如下:Rank by Age                 FirstName         Age
-------------------- ---------- -----------
1                           Larry             5
2                           Doris             6
2                           George            6
4                           Mary              11
4                           Sherry            11
6                           Sam               17
7                           Ted               23
7                           Marty             23
9                           Sue               29
10                          Frank             38
11                          John              40
看到了么,同年岭的话,将有相同的顺序,顺序成1,2,2,4了。
sql server2000对比:出现了RANK()函数实在是方便,在sql server2000里实现排序并列的问题麻烦很多。select [Rank by Age]=isnull((select count(*) from person where Age>A.Age),0)+1,FirstName,Age from Person A order by [Rank by Age]
 
SELECT RANK() OVER(PARTITION BY Gender ORDER BY Age) AS [Partition by Gender],FirstName, Age, Gender FROM Person输出为
Partition by Gender         FirstName         Age                Gender
-------------------- ---------- ----------- ------
1                           Doris             6                  F
2                           Mary              11                 F
2                           Sherry            11                 F
4                           Sue               29                 F
1                           Larry             5                  M
2                           George            6                  M
3                           Sam               17                 M
4                           Ted               23                 M
4                           Marty             23                 M
6                           Frank             38                 M
7                           John              40                 M

可以看到,按性别分组了,每个性别分组里,继续是用了rank( )函数

3.DENSE_RANK( )函数
         SELECT DENSE_RANK() OVER (ORDER BY Age) AS [Dense Rank by Age], 
         FirstName, 
         Age
         FROM Person

输出结果为:
Dense Rank by Age          FirstName        Age
-------------------- ---------- -----------
1                          Larry            5
2                          Doris            6
2                          George           6
3                          Mary             11
3                          Sherry           11
4                          Sam              17
5                          Ted              23
5                          Marty            23
6                          Sue              29
7                          Frank            38
8                          John             40

看到了么,和rank函数区别是,顺序始终是连续的,Doris 和George同年,都是排第2位,但之后的mary不象rank函数那样排第4,而是排第3位了

4.ntile( )函数
SELECT FirstName,
Age,
NTILE(3) OVER (ORDER BY Age) AS [Age Groups]
FROM Person

输出结果:
FirstName        Age               Age Groups
---------- ----------- --------------------
Larry                5                  1
Doris                6                  1
George            6                  1
Mary                11                1
Sherry             11                 2
Sam                17                 2
Ted                 23                 2
Marty              23                 2
Sue                29                 3
Frank             38                 3
John               40                 3
这个函数按照ntile(n)中的N,把记录强制分成多少段,11条记录现在分成3段了,lary到mary是第1段,sherry到maty是第2段,sue到john是第3段了。

时间: 2024-10-26 02:41:19

sql server 2000 对应 sql server 2005的row_number()、rank()、DENSE_RANK( )、ntile( )等用法的相关文章

sql server 2000 和 sql server 2005 数据库连接字符串区别

//sql server 2000 <add name="Connection" connectionString="Data Source=.;Initial Catalog=DB;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/> //sql server 2005 <add name="Connection" connection

SQL Server 2000向SQL Server 2008 R2推送数据

[文章摘要]最近做的一个项目要获取存在于其他服务器的一些数据,为了安全起见,采用由其他“服务器”向我们服务器推送的方式实现.我们服务器使用的是SQL Server 2008 R2,其他“服务器”使用的都是SQL Server 2000,还都是运行在Windows XP上的,整个过程遇到了一些问题,也参考了一些文档,最终费了好多事才算搞定. [文章索引] 配置分发服务器 配置发布数据库 配置订阅 [一.配置分发服务器] SQLServer 2000的复制服务包括三个角色:发布服务器.分发服务器和订

sql排序对比(row_number,rank,dense_rank)

不解释,全靠悟性: select loan_num,row_number() over(order by loan_num asc) as 'Row Number',rank() over(order by loan_num asc) as 'Rank',dense_rank() over(order by loan_num asc) as 'Dense Rank'from dbo.tals_loan

SQL SERVER 2000安装教程图文详解

注意:Windows XP不能装企业版.win2000\win2003服务器安装企业版一.硬件和操作系统要求 下表说明安装 Microsoft SQL Server 2000 或 SQL Server 客户端管理工具和库的硬件要求. 硬件 最低要求计算机 Pentium 166 MHz 或更高. 内存 (RAM)至少 64 MB,建议 128 MB 或更多.根据笔者的经验,内存容量可以和数据容量保持1:1的比例,这样可以更好的发挥其效能.硬盘空间需要约500MB的程序空间,以及预留500M的数据

sql server 2000/2005递归

/* 递归查询 塗聚文---SQL Server 2005环境下的实现: */--生成测试数据 create table Dept(ID int,ParentID int,msg varchar(20)) insert into Dept select 1,0,'d' insert into Dept select 2,1,'s' insert into Dept select 3,1,'nl' insert into Dept select 4,2,'d' insert into Dept s

SQL Server 2012不支持从SQL Server 2000的备份进行还原

错误: dbbackup failed: Unable to restore database 'ppt'Not valid backupThe database was backed up on a server running version 8.00.2039. That version is incompatible with this server, which is running version 11.00.3000. Either restore the database on

SQL server 2000 Agent 不能随系统 自动启动

声明:本博客只作技术分享,不作为推广本人的目的. ----------------------------------------------------------------------------------- 最近帮公司财务电脑从Windows XP升级到 Windows7/8中, 解决了 Windows 7/8 安装财务报税相关软件遇到的问题: 要SQL server 2000作为本地存储,最后的解决方案是安装SQL Server 2005/2008/2012即可以,本人下载了SQL

Java连接SQL Server 2000 (version 8)

对于古老的SQL Server 2000,连接方法有点特别. 1.下载SQL JDBC 驱动,3.0版 的jar包(一定要3.0版的,4.0版的只能用在SQL server 2005版以上,而且Maven Repository找不到的,只能自己加入本地仓库或直接放入项目中) http://download.csdn.net/detail/clementad/8862363 2..导入jar包到本地仓库或项目目录中 如果放入项目目录中,则在pom.xml文件中添加: <dependency> &

怎么查看SQL SERVER 2000是否打过SP4的补丁

怎么查看SQL SERVER 2000是否打过SP4的补丁 其实只要查询版本号就知道.在查询分析器中输入 select@@version 或者输入 print @@version 如果是安装过SP4的补丁.应该是下面的信息: Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)May 3 2005 23:18:38Copyright (c) 1988-2003 Microsoft CorporationEnterprise Edition on Wi