December 2013
Intermediate to advanced
1872 pages
153h 31m
English
The OVER clause can be used with standard aggregate functions over all the rows returned by a query while generating aggregate values in the result along with the detail rows. The PARTITION BY clause is used to specify the subgroups within the result set base for which the aggregates should be calculated, as shown in the following example:
SELECT SalesOrderID, ProductID, OrderQty ,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS Total ,AVG(OrderQty) OVER(PARTITION BY SalesOrderID) AS "Avg" ,COUNT(OrderQty) OVER(PARTITION BY SalesOrderID) AS "Count" ,MIN(OrderQty) OVER(PARTITION BY SalesOrderID) AS "Min" ,MAX(OrderQty) OVER(PARTITION BY SalesOrderID) AS "Max" ...