DECLARE @TestData TABLE(
ID INT IDENTITY(1,1),
Department VARCHAR(20),
LastName VARCHAR(20),
Rate FLOAT
)
INSERT INTO @TestData(Department,LastName,Rate)
SELECT 'Document Control','Arifin',17.7885 UNION ALL
SELECT 'Document Control','Norred',16.8269 UNION ALL
SELECT 'Document Control','Kharatishvili',16.8269 UNION ALL
SELECT 'Information Services','Chai',10.25 UNION ALL
SELECT 'Information Services','Berge',10.25 UNION ALL
SELECT 'Information Services','Trenary',50.4808 UNION ALL
SELECT 'Information Services','Conroy',39.6635 UNION ALL
SELECT 'Information Services','Ajenstat',38.4615 UNION ALL
SELECT 'Information Services','Wilson',38.4615 UNION ALL
SELECT 'Information Services','Connelly',32.4519 UNION ALL
SELECT 'Information Services','Meyyappan',32.4519
SELECT * FROM @TestData
下邊使用FIRST_VALUE函數(shù),創(chuàng)建一列新列,返回結(jié)果集中第一行的LastName值,這個(gè)所謂的第一行受OVER里的ORDER BY影響,看圖和代碼:
如果SQL腳本中使用了PARTITION分區(qū)函數(shù),則FIRST_VALUE返回每個(gè)分區(qū)內(nèi)的首條數(shù)據(jù)值,看演示
這里以Department分區(qū),則整個(gè)數(shù)據(jù)集被分成了兩部分:Information Services和Document Control兩塊,這時(shí)FIRST_VALUE分別返回兩塊分區(qū)內(nèi)的首條數(shù)據(jù)值,同樣的受ORDER BY關(guān)鍵字的影響,
與FIRST_VALUE函數(shù)同時(shí)出現(xiàn)的還有一個(gè),SECOND_VALUE?NO,沒有這個(gè)函數(shù)啊,但是有一LAST_VALUE,怎么函數(shù)怎么使用,不打算再單獨(dú)起一篇文章了,LAST_VALUE嗯