This is a piece of code on MSDN page for OVER clause:
USE AdventureWorks2012;
GO
SELECT BusinessEntityID, TerritoryID
,DATEPART(yy,ModifiedDate) AS SalesYear
,CONVERT(varchar(20),SalesYTD,1) AS SalesYTD
,CONVERT(varchar(20),AVG(SalesYTD) OVER (PARTITION BY TerritoryID
ORDER BY DATEPART(yy,ModifiedDate)
),1) AS MovingAvg
,CONVERT(varchar(20),SUM(SalesYTD) OVER (PARTITION BY TerritoryID
ORDER BY DATEPART(yy,ModifiedDate)
),1) AS CumulativeTotal
FROM Sales.SalesPerson
WHERE TerritoryID IS NULL OR TerritoryID < 5
ORDER BY TerritoryID,SalesYear;
So I am having issues understanding why CONVERT function had to be used there. I assume it is to do with return types of one of the fields in the expression part of the convert function? Can CAST be used instead?
Second question I have is, how exactly does this part work?
SUM(SalesYTD) OVER (PARTITION BY TerritoryID
ORDER BY DATEPART(yy,ModifiedDate)
What exactly is that saying? Is it calculating sum for every year? Is that it?
Best Answer
If SalesYTD type is float or real:
1 = Always 8 digits. Always use in scientific notation.
If type is money and small money:
1 = Commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 3,510.92.
If SalesYTD type is float or real:
0 (default) = A maximum of 6 digits. Use in scientific notation, when appropriate.
If type is money and small money:
0 (default) = No commas every three digits to the left of the decimal point, and two digits to the right of the decimal point; for example, 4235.98.
There are other differences and default values for style with other types but I think it covers your data types. See CAST and CONVERT (Transact-SQL)
For each partition (=TerritoryID) it takes all SalesYTD, order them by DATEPART(yy,ModifiedDate) and sum them. Note that the Order part is not necessary or could be anything since you only do SUM and AVG but using it change the behaviors (see hypercube answer about it and moving average). With ROW_NUMBER and others, it would be important and mandatory.
Diffence between SUM and moving SUM (@hypercube answer):
Output:
See OVER Clause (Transact-SQL) for window functions.