Running Aggregations

Running aggregations are aggregations of data over a sequence (typically temporal). Running aggregate problems have many variations, and I’ll describe several important ones here.

In my examples, I’ll use a summary table called EmpOrders that contains one row for each employee and month, with the total quantity of orders made by that employee in that month. Run the following code to create the EmpOrders table and populate it with sample data:

USE tempdb; IF OBJECT_ID('dbo.EmpOrders') IS NOT NULL DROP TABLE dbo.EmpOrders; CREATE TABLE dbo.EmpOrders ( empid INT NOT NULL, ordmonth DATE NOT NULL, qty INT NOT NULL, PRIMARY KEY(empid, ordmonth) ); GO INSERT INTO dbo.EmpOrders(empid, ordmonth, qty) SELECT O.empid, DATEADD(month, DATEDIFF(month, ...

Get Inside Microsoft® SQL Server® 2008: T-SQL Querying now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.