回覆列表
-
1 # 三木四水90056261
-
2 # 一絲不苟圓月xyz
關於lead函式:
lead函式的作用是:可以在一次查詢中取出當前行的同一欄位(field引數)的後面第num行的資料,如果沒有用defaultvalue代替。透過這個函式可以在一次查詢中取出同一欄位的前N行的資料(lag)和後N行的資料。(lead)作為獨立的列,從而更方便地進行進行資料過濾,該操作可代替表的自聯接,且效率更高。
LEAD函式簡單點說,就是把下一行的某列資料提取到當前行來顯示,看示例更能解釋清楚:
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