北京北大青鳥指導(dǎo):SQL2005中新增函數(shù)的用法


SQL server 2005新增的幾個(gè)函數(shù),分別是row_number( )、rank( )、,DENSE_RANK( )、ntile( )下面,北京北大青鳥通州校區(qū)ACCP學(xué)術(shù)部老師就以實(shí)例分別簡單講解。

1.row_number()
先來點(diǎn)數(shù)據(jù),先建個(gè)表

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
出現(xiàn)的數(shù)據(jù)如下
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可以觀察到,是根據(jù)年齡升序排列了,并且row_number()是給出了序列號(hào)了,這個(gè)序列號(hào)被重命名為Row Number by Age,與sql server2000對(duì)比:
如果在sql server2000中實(shí)現(xiàn)相對(duì)麻煩一些,我們可以利用IDENTITY()函數(shù)實(shí)現(xiàn),但I(xiàn)DENTITY()函數(shù)只能用在sql server2000臨時(shí)表中,因此需要將數(shù)據(jù)檢索到臨時(shí)表里。
select identity(int,1,1) as [Row Number by Age],FirstName,Age into #A from Person order by Age
select * from #A
drop table #a如果不想按年齡排序,可以這樣寫
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS [Row Number by Record Set], FirstName,Age FROM Person另外一個(gè)例子
SELECT ROW_NUMBER() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender], FirstName,Age,GenderFROM Person這里是按性別劃分區(qū)間了,同一性別再按年齡來排序,輸出結(jié)果如下
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開始,序號(hào)又從1,2,3開始了

2.RANK( )函數(shù)
先看例子
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對(duì)比:
出現(xiàn)了RANK()函數(shù)實(shí)在是方便,在sql server2000里實(shí)現(xiàn)排序并列的問題麻煩很多。
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
可以看到,按性別分組了,每個(gè)性別分組里,繼續(xù)是用了rank( )函數(shù)

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

輸出結(jié)果為:
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函數(shù)區(qū)別是,順序始終是連續(xù)的,Doris 和George同年,都是排第2位,但之后的mary不象rank函數(shù)那樣排第4,而是排第3位了


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

輸出結(jié)果:
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
這個(gè)函數(shù)按照ntile(n)中的N,把記錄強(qiáng)制分成多少段,11條記錄現(xiàn)在分成3段了,lary到mary是第1段,sherry到maty是第2段,sue到j(luò)ohn是第3段了。

北大青鳥網(wǎng)上報(bào)名
北大青鳥招生簡章