如何计算SQL Server 2008中的运行总计(月初至今)

时间:2013-12-29 21:35:35

标签: sql sql-server tsql aggregation running-total

我正在尝试使用SQL Server 2008计算月初至今的总数。

我正在尝试在活动和代表级别生成一个月初至今的计数。以下是我想要生成的结果:

| 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 |

从下表中:

ACTIVITIES_FACT表

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

LU_TIME表

+-------+-----------------+--------+  
| 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   |  
+-------+-----------------+--------+ 

我不知道如何做到这一点:我一直遇到子查询中不允许多次计数或聚合的问题。谢谢你的帮助。

3 个答案:

答案 0 :(得分:4)

  

running total是一系列数字的总和   每次将新数字添加到序列时更新,只需通过   将新号码的值添加到运行总计中。

认为他想要每个代表_Id的月份运行总计,所以一个简单的group by周是不够的。他可能希望他的Month_To_Date_Activities_Count在每周结束时更新。

此查询提供按行代表_Id,周

排序的运行总计(月份到周末日期)
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

答案 1 :(得分:3)

据我了解你的问题:

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

答案 2 :(得分:3)

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