Question

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:

| REPRESENTATIVE_ID | MONTH | WEEK | TOTAL_WEEK_ACTIVITY_COUNT | MONTH_TO_DATE_ACTIVITIES_COUNT |
|-------------------|-------|------|---------------------------|--------------------------------|
|                40 |     7 | 7/08 |                         1 |                              1 |
|                40 |     8 | 8/09 |                         1 |                              1 |
|                40 |     8 | 8/10 |                         1 |                              2 |
|                41 |     7 | 7/08 |                         2 |                              2 |
|                41 |     8 | 8/08 |                         4 |                              4 |
|                41 |     8 | 8/09 |                         3 |                              7 |
|                41 |     8 | 8/10 |                         1 |                              8 |

From the following tables:

ACTIVITIES_FACT table

+-------------------+------+-----------+  
| Representative_ID | Date | Activity  |  
+-------------------+------+-----------+  
|                41 | 8/03 | Call      |  
|                41 | 8/04 | Call      |  
|                41 | 8/05 | Call      |  
+-------------------+------+-----------+  

LU_TIME table

+-------+-----------------+--------+  
| Month | Date            | Week   |  
+-------+-----------------+--------+  
|     8 | 8/01            | 8/08   |  
|     8 | 8/02            | 8/08   |  
|     8 | 8/03            | 8/08   |  
|     8 | 8/04            | 8/08   |  
|     8 | 8/05            | 8/08   |  
+-------+-----------------+--------+ 

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

Was it helpful?

Solution

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

SELECT a.Representative_ID, l.month, l.Week, Count(*) AS Total_Week_Activity_Count
    ,(SELECT  count(*)
        FROM ACTIVITIES_FACT a2
        INNER JOIN LU_TIME l2 ON a2.Date = l2.Date
        AND a.Representative_ID = a2.Representative_ID
        WHERE l2.week <=  l.week
        AND l2.month = l.month) Month_To_Date_Activities_Count
FROM ACTIVITIES_FACT a
INNER JOIN LU_TIME l ON a.Date = l.Date
GROUP BY a.Representative_ID, l.Week, l.month
ORDER BY a.Representative_ID, l.Week

| REPRESENTATIVE_ID | MONTH | WEEK | TOTAL_WEEK_ACTIVITY_COUNT | MONTH_TO_DATE_ACTIVITIES_COUNT |
|-------------------|-------|------|---------------------------|--------------------------------|
|                40 |     7 | 7/08 |                         1 |                              1 |
|                40 |     8 | 8/09 |                         1 |                              1 |
|                40 |     8 | 8/10 |                         1 |                              2 |
|                41 |     7 | 7/08 |                         2 |                              2 |
|                41 |     8 | 8/08 |                         4 |                              4 |
|                41 |     8 | 8/09 |                         3 |                              7 |
|                41 |     8 | 8/10 |                         1 |                              8 |

SQL Fiddle Sample

OTHER TIPS

As I understand your question:

SELECT af.Representative_ID
, lt.Week
, COUNT(af.Activity) AS Qnt
FROM ACTIVITIES_FACT af
INNER JOIN LU_TIME lt ON lt.Date = af.date
GROUP BY af.Representative_ID, lt.Week

SqlFiddle

Representative_ID   Week                           Month_To_Date_Activities_Count
 41                 2013-08-01 00:00:00.000        1
 41                 2013-08-08 00:00:00.000        3


 USE tempdb;
GO
IF OBJECT_ID('#ACTIVITIES_FACT','U') IS NOT NULL DROP TABLE #ACTIVITIES_FACT;
CREATE TABLE #ACTIVITIES_FACT
(
 Representative_ID INT NOT NULL
,Date DATETIME NULL
, Activity VARCHAR(500) NULL
)

IF OBJECT_ID('#LU_TIME','U') IS NOT NULL DROP TABLE #LU_TIME;
CREATE TABLE #LU_TIME
(
 Month INT
,Date DATETIME
,Week DATETIME
)

INSERT INTO #ACTIVITIES_FACT(Representative_ID,Date,Activity)
VALUES
 (41,'7/31/2013','Chat')
,(41,'8/03/2013','Call')
,(41,'8/04/2013','Call')
,(41,'8/05/2013','Call')

INSERT INTO #LU_TIME(Month,Date,Week)
VALUES
 (8,'7/31/2013','8/01/2013')
,(8,'8/01/2013','8/08/2013')
,(8,'8/02/2013','8/08/2013')
,(8,'8/03/2013','8/08/2013')
,(8,'8/04/2013','8/08/2013')
,(8,'8/05/2013','8/08/2013')
--Begin Query
SELECT      AF.Representative_ID
            ,LU.Week
            ,COUNT(*) AS Month_To_Date_Activities_Count
FROM        #ACTIVITIES_FACT AS AF
INNER JOIN  #LU_TIME AS LU
    ON      AF.Date = LU.Date
Group By    AF.Representative_ID
            ,LU.Week
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top