funzioni aggregate in OVER
clausola può essere riscritto in questo modo: FROM table AS x INNER JOIN (SELECT partition columns, AggregateWithoutOverClause (...) ... FROM ...) AS y ON x.PartitionColumns = y.PartitionColumns (se le colonne di partizione sono obbligatorie - NOT NULL).
Esempio:
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;
Risultati:
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.
I piani di esecuzione:
Questo sarà solo spiegare l'ultima unirsi:
La spiegazione per è la prima volta in nella sezione Elaborazione per gruppo/Partitioning and the Common Subexpression Spool.
fonte
2013-04-14 15:00:05