在SQL表中汇总(永久)数据

时间:2010-03-11 16:15:11

标签: sql sql-server-2005

Geetings,Stackers。

我在SQL表中有大量的数据点,我想以一种让人想起RRD的方式对它们进行总结。

假设有一个表格,如

 ID | ENTITY_ID | SCORE_DATE | SCORE | SOME_OTHER_DATA
----+-----------+------------+-------+-----------------
  1 | A00000001 | 01/01/2010 |   100 | some data
  2 | A00000002 | 01/01/2010 |   105 | more data
  3 | A00000003 | 01/01/2010 |   104 | various text
... | ......... | .......... | ..... | ...
... | A00009999 | 01/01/2010 |   101 | 
... | A00000001 | 02/01/2010 |   104 | 
... | A00000002 | 02/01/2010 |   119 | 
... | A00000003 | 02/01/2010 |   119 | 
... | ......... | .......... | ..... | 
... | A00009999 | 02/01/2010 |   101 | arbitrary data
... | ......... | .......... | ..... | ...
... | A00000001 | 01/02/2010 |   104 | 
... | A00000002 | 01/02/2010 |   119 | 
... | A00000003 | 01/01/2010 |   119 | 

我希望每个实体最终每月有一条记录:

 ID | ENTITY_ID | SCORE_DATE | SCORE |
----+-----------+------------+-------+
... | A00000001 | 01/01/2010 |   100 |
... | A00000002 | 01/01/2010 |   105 |
... | A00000003 | 01/01/2010 |   104 |
... | A00000001 | 01/02/2010 |   100 |
... | A00000002 | 01/02/2010 |   105 |
... | A00000003 | 01/02/2010 |   104 |

(我不关心SOME_OTHER_DATA - 我会选择一些东西 - 可能是第一个或最后一个记录。)

定期执行此操作的简单方法是什么,以便以上述方式汇总上一个日历月中的任何内容?

目前我的计划有点:

  • 对于每个EntityID
    • 每个月
      • 查找给定月份内所有记录的平均分数
      • 使用上一步的结果更新第一条记录
      • 删除所有不是第一个
      • 的记录

我想不出一种干净利落的方式,但这并不涉及大量的更新和迭代。

这可以在SQL存储过程中完成,也可以合并到生成此数据的.Net应用程序中,因此解决方案实际上不需要是“一个大的SQL脚本”,但可以是: )

(SQL-2005)

2 个答案:

答案 0 :(得分:0)

尝试一下:

--I am using @table variables here, you will want to use your actual table in place of @YourTable and a #Temptable for @YourTable2, with a PK on ID
SET NOCOUNT ON
DECLARE @YourTable table (ID int,ENTITY_ID char(9),SCORE_DATE datetime,SCORE int ,SOME_OTHER_DATA varchar(100))
DECLARE @YourTable2 table (ID int)
INSERT INTO @YourTable VALUES (1 , 'A00000001','01/01/2010',100,'some data')
INSERT INTO @YourTable VALUES (2 , 'A00000002','01/01/2010',105,'more data')
INSERT INTO @YourTable VALUES (3 , 'A00000003','01/01/2010',104,'various text')
INSERT INTO @YourTable VALUES (4 , 'A00009999','01/01/2010',101,null)
INSERT INTO @YourTable VALUES (5 , 'A00000001','02/01/2010',104,null)
INSERT INTO @YourTable VALUES (6 , 'A00000002','02/01/2010',119,null)
INSERT INTO @YourTable VALUES (7 , 'A00000003','02/01/2010',119,null)
INSERT INTO @YourTable VALUES (8 , 'A00009999','02/01/2010',101,'arbitrary data')
INSERT INTO @YourTable VALUES (9 , 'A00000001','01/02/2010',104,null)
INSERT INTO @YourTable VALUES (10, 'A00000002','01/02/2010',119,null)
INSERT INTO @YourTable VALUES (11, 'A00000003','01/01/2010',119,null)
SET NOCOUNT OFF

SELECT 'BEFORE',* FROM @YourTable ORDER BY ENTITY_ID,SCORE_DATE

UPDATE y
    SET SCORE=dt_a.AvgScore
    OUTPUT INSERTED.ID   --capture all updated rows
        INTO @YourTable2
    FROM @YourTable y
        INNER JOIN (SELECT --get avg score for each ENTITY_ID per month
                        ENTITY_ID
                            ,AVG(SCORE) as AvgScore
                            , DATEADD(month,DATEDIFF(month,0,SCORE_DATE),0) AS MonthOf,DATEADD(month,1,DATEADD(month,DATEDIFF(month,0,SCORE_DATE),0)) AS MonthNext
                        FROM @YourTable
                        --group by 1st day  of current month and 1st day of next month
                        --so an index can be used when joining derived table to UPDATE table
                        GROUP BY ENTITY_ID, DATEADD(month,DATEDIFF(month,0,SCORE_DATE),0),DATEADD(month,1,DATEADD(month,DATEDIFF(month,0,SCORE_DATE),0))
                   ) dt_a ON y.ENTITY_ID=dt_a.ENTITY_ID AND y.SCORE_DATE>=dt_a.MonthOf AND y.SCORE_DATE<dt_a.MonthNext
        INNER JOIN (SELECT--get first row for each ENTITY_ID per month
                        ID,ENTITY_ID,SCORE_DATE,SCORE
                        FROM (SELECT
                                  ID,ENTITY_ID,SCORE_DATE,SCORE
                                      ,ROW_NUMBER() OVER(PARTITION BY ENTITY_ID,DATEADD(month,DATEDIFF(month,0,SCORE_DATE),0) ORDER BY ENTITY_ID,SCORE_DATE) AS RowRank
                                  FROM @YourTable
                             ) dt
                        WHERE dt.RowRank=1
                   ) dt_f ON y.ID=dt_f.ID

DELETE @YourTable
    WHERE ID NOT IN (SELECT ID FROM @YourTable2)


SELECT 'AFTER ',* FROM @YourTable ORDER BY ENTITY_ID,SCORE_DATE

输出:

       ID          ENTITY_ID SCORE_DATE              SCORE       SOME_OTHER_DATA
------ ----------- --------- ----------------------- ----------- ----------------------------------------------------------------------------------------------------
BEFORE 1           A00000001 2010-01-01 00:00:00.000 100         some data
BEFORE 9           A00000001 2010-01-02 00:00:00.000 104         NULL
BEFORE 5           A00000001 2010-02-01 00:00:00.000 104         NULL
BEFORE 2           A00000002 2010-01-01 00:00:00.000 105         more data
BEFORE 10          A00000002 2010-01-02 00:00:00.000 119         NULL
BEFORE 6           A00000002 2010-02-01 00:00:00.000 119         NULL
BEFORE 3           A00000003 2010-01-01 00:00:00.000 104         various text
BEFORE 11          A00000003 2010-01-01 00:00:00.000 119         NULL
BEFORE 7           A00000003 2010-02-01 00:00:00.000 119         NULL
BEFORE 4           A00009999 2010-01-01 00:00:00.000 101         NULL
BEFORE 8           A00009999 2010-02-01 00:00:00.000 101         arbitrary data

(11 row(s) affected)

(8 row(s) affected)

(3 row(s) affected)

       ID          ENTITY_ID SCORE_DATE              SCORE       SOME_OTHER_DATA
------ ----------- --------- ----------------------- ----------- ----------------------------------------------------------------------------------------------------
AFTER  1           A00000001 2010-01-01 00:00:00.000 102         some data
AFTER  5           A00000001 2010-02-01 00:00:00.000 104         NULL
AFTER  2           A00000002 2010-01-01 00:00:00.000 112         more data
AFTER  6           A00000002 2010-02-01 00:00:00.000 119         NULL
AFTER  3           A00000003 2010-01-01 00:00:00.000 111         various text
AFTER  7           A00000003 2010-02-01 00:00:00.000 119         NULL
AFTER  4           A00009999 2010-01-01 00:00:00.000 101         NULL
AFTER  8           A00009999 2010-02-01 00:00:00.000 101         arbitrary data

(8 row(s) affected)

答案 1 :(得分:0)

这将为您提供所有数据的平均值:

select ENTITY_ID, year(SCORE_DATE) as Year, month(SCORE_DATE) as Month, avg(SCORE) as Avg
from MyTable
group by ENTITY_ID, year(SCORE_DATE), month(SCORE_DATE)

要限制到某个月,例如去年2月,您可以执行以下操作:

select ENTITY_ID, year(SCORE_DATE) as Year, month(SCORE_DATE) as Month, avg(SCORE) as Avg
from MyTable
where year(SCORE_DATE) = 2010 and month(SCORE_DATE) = 2
group by ENTITY_ID, year(SCORE_DATE), month(SCORE_DATE)

这个版本实际上会表现得更好,但参数处理起来不太友好:

select ENTITY_ID, year(SCORE_DATE) as Year, month(SCORE_DATE) as Month, avg(SCORE) as Avg
from MyTable
where SCORE_DATE >= '2/1/2010' and SCORE_DATE < '3/1/2010'
group by ENTITY_ID, year(SCORE_DATE), month(SCORE_DATE)

如果您想要一个始终返回上个月数据的查询,您可以这样做:

select ENTITY_ID, year(SCORE_DATE) as Year, month(SCORE_DATE) as Month, avg(SCORE) as Avg
from MyTable
where year(SCORE_DATE) = year(dateadd(month, -1, getdate())) and month(dateadd(month, -1, getdate())) = 2
group by ENTITY_ID, year(SCORE_DATE), month(SCORE_DATE)

性能更佳的版本:

select ENTITY_ID, year(SCORE_DATE) as Year, month(SCORE_DATE) as Month, avg(SCORE) as Avg
from MyTable
where SCORE_DATE >= dateadd(month, ((year(getdate()) - 1900) * 12) + month(getdate())-2, 0) 
    and SCORE_DATE < dateadd(month, ((year(getdate()) - 1900) * 12) + month(getdate())-1, 0)
group by ENTITY_ID, year(SCORE_DATE), month(SCORE_DATE)