日韩欧美亚州-日韩欧美亚洲国产-日韩欧美亚洲国产高清在线-日韩欧美亚洲精品-色135综合网-色91在线

北京北大青鳥指導:SQL2005中新增函數的用法


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

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 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另外一個例子
SELECT ROW_NUMBER() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender], FirstName,Age,GenderFROM 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段了。

北大青鳥網上報名
北大青鳥招生簡章
主站蜘蛛池模板: 亚洲国产成人99精品激情在线| 久久女厕一次看个够| 成人高清在线观看| 久久免费小视频| 最新日韩欧美不卡一二三区| 久久黄色片| 免费在线看a| 亚洲一区在线视频观看| 国产精品情人露脸在线观看| 久久中文字幕日韩精品| 永久网站色视频在线观看免费| 国产亚洲精品影达达兔| 美国一级毛片片aaa| 欧美性色黄大片一级毛片视频| 国产成人在线小视频| 日本欧美视频| 性刺激欧美三级在线现看中文| 国产精选一区| 欧美高清在线视频在线99精品| 偷拍自拍视频在线| aaa级大片| 国产欧美日韩图片一区二区| 久久综合久久久久| 亚洲国产日韩欧美在线| 在线免费视频国产| 成人1000部免费观看视频| 国模肉肉人体大尺度啪啪| 九九在线观看视频| 欧美手机看片| 免费一级毛片私人影院a行| 曰本三级| 一级国产a级a毛片无卡| 成人a毛片免费全部播放| 国产免费观看a大片的网站| 精品国产一区二区三区在线| 日本免费一区尤物| 欧美成人性毛片免费版| 亚洲国产综合久久精品| 日本韩国一级片| 亚洲加勒比久久88色综合1| 窝窝午夜看片七次郎青草视频|