I have a series on window functions:
Here, we get the sum of
LineProfit
byCustomerID
. BecauseSUM()
is an aggregate function, we need aGROUP BY
clause for all non-aggregated columns. This is an aggregate function. The full set of them in T-SQL is available here, but you’ll probably be most familiar withMIN()
,MAX()
,SUM()
,AVG()
, andCOUNT()
.To turn this into a window function, we slap on an
OVER()
and boom! Note: “boom!” only works on SQL Server 2012 and later, so if you’re still on 2008 R2, it’s more of a fizzle than a boom.
Read on for several examples of this nature.