Skip to content
Advertisement

How to calculate running total (month to date) in SQL Server 2008

I’m trying to calculate a month-to-date total using SQL Server 2008.

I’m trying to generate a month-to-date count at the level of activities and representatives. Here are the results I want to generate:

From the following tables:

ACTIVITIES_FACT table

LU_TIME table

I’m not sure how to do this: I keep running into problems with multiple-counting or aggregations not being allowed in subqueries.

Advertisement

Answer

A running total is the summation of a sequence of numbers which is updated each time a new number is added to the sequence, simply by adding the value of the new number to the running total.

I THINK He wants a running total for Month by each Representative_Id, so a simple group by week isn’t enough. He probably wants his Month_To_Date_Activities_Count to be updated at the end of every week.

This query gives a running total (month to end-of-week date) ordered by Representative_Id, Week


SQL Fiddle Sample

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement