Tuesday, December 04, 2012

Over Clause vs Group By Clause

It used to determine the partitioning and ordering of a row set before the associated window function is applied. The window function includes Ranking functions, Aggregation functions, Analytic functions etc.

In SQL Server 2012, You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results. Before that, only Ranking function can work with Order By.

(Order By in OVER happens before the ORDER BY clause in a SELECT statement)

Aggregation functions working with GROUP BY aggregates the result set according to the columns in GROUP BY, and the result set is presented according to GROUP BY.

OVER works with a window of result set, it provides the running aggregations over the window and for each of the row in the window. It replaces what we have to do with sub-queries and it is more efficient than sub-queries.

No comments: