chức năng tổng hợp với OVER
khoản có thể được viết lại như sau: TỪ bảng AS x INNER JOIN (các cột phân vùng SELECT, AggregateWithoutOverClause (...) ... FROM ...) AS y ON x.PartitionColumns = y.PartitionColumns (nếu các cột phân vùng là bắt buộc - NOT NULL).
Ví dụ:
SET STATISTICS IO ON;
SET NOCOUNT ON;
-- OP's query
SELECT sal,sum(sal) over(PARTITION BY empno)
FROM emp;
-- Reqwriten query
SELECT a.sal, b.SumSal
FROM emp a
INNER JOIN (SELECT EMPNO, SUM(sal) AS SumSal FROM emp GROUP BY EMPNO) b ON a.EMPNO = b.EMPNO;
Kết quả:
sal
----------- -----------
1 10
2 10
3 10
4 10
1 3
2 3
Table 'Worktable'. Scan count 3, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'EMP'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
sal SumSal
----------- -----------
1 10
2 10
3 10
4 10
1 3
2 3
Table 'Worktable'. Scan count 3, logical reads 21, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'EMP'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
kế hoạch Thực hiện: ![enter image description here](https://i.stack.imgur.com/oSaLj.png)
này sẽ giải thích chỉ tham gia cuối cùng: ![enter image description here](https://i.stack.imgur.com/CIHI9.png)
Giải thích cho số lần đầu tiên tham gia có thể được tìm thấy trong phần Xử lý theo nhóm/Partitioning and the Common Subexpression Spool.
Nguồn
2013-04-14 15:00:05