SQL Server 2012 開窗函數。本站提示廣大學習愛好者:(SQL Server 2012 開窗函數)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL Server 2012 開窗函數正文
廢話不多說了,直接給大家貼代碼了,詳細代碼如下所示:
-- 開窗函數:在後果集的根底上進一步處置(聚合操作) -- Over函數,添加一個字段顯示最大年齡 SELECT * , MAX(StuAge) OVER ( ) MaxStuAge FROM dbo.Student; -- Over函數,添加一個字段顯示總人數 SELECT * , COUNT(StuID) OVER ( ) StuCount FROM dbo.Student; -- Partition By 分組統計數量 -- 依據性別分組後,統計 SELECT COUNT(*) OVER ( PARTITION BY StuSex ) , * FROM dbo.Student; -- 依據班級分組後,統計、排序 SELECT COUNT(*) OVER ( PARTITION BY Class ORDER BY Height) , * FROM dbo.Student; -- Over函數,添加一個字段顯示均勻身高 SELECT * , AVG(Height) OVER ( ) AgeHeight FROM dbo.Student; --Row_Rumber() SELECT ROW_NUMBER() OVER ( ORDER BY StuID DESC ) RowNumber , * FROM dbo.Student --Row_Rumber() 完成分頁效果 ; WITH T AS ( SELECT ROW_NUMBER() OVER ( ORDER BY StuID DESC ) RowNumber , * FROM dbo.Student ) SELECT * FROM T WHERE T.RowNumber BETWEEN 1 AND 3; --Rank() 排名函數,名次相反,跳過 SELECT RANK() OVER ( ORDER BY Height ) , * FROM dbo.Student; --DENSE_Rank() 排名函數,名次相反不跳過 SELECT DENSE_RANK() OVER ( ORDER BY Height ) , * FROM dbo.Student; -- NTILE()函數,參數:記載總數/劃分區域 = 每個區域數組,把記載序號放進數組 (均勻分組) SELECT NTILE(3) OVER ( ORDER BY StuSex ) , * FROM dbo.Student;
以上所述是給大家引見的SQL Server 2012 開窗函數,希望對大家有所協助,假如大家有任何疑問歡送給我留言,會及時回復大家的!