SQL-2008 SUM(X)OVER(PARTITION ... ORDER BY CLAUSE)

时间:2013-10-30 13:55:42

标签: sql sql-server-2008 tsql running-total over-clause

我真的需要拿走CTE的结果,并计算数据组的累积值。

数据集是:

PERIOD  FT  GROUP   DEPT    VALUE
1   Actual  KINDER  MATH    200
2   Actual  KINDER  MATH    363
3   Actual  KINDER  MATH    366
1   Budget  KINDER  MATH    457
2   Budget  KINDER  MATH    60
3   Budget  KINDER  MATH    158
1   Actual  HIGHSCH ENGLISH 456
2   Actual  HIGHSCH ENGLISH 745
3   Actual  HIGHSCH ENGLISH 125
1   Budget  HIGHSCH ENGLISH 364
2   Budget  HIGHSCH ENGLISH 158
3   Budget  HIGHSCH ENGLISH 200
6   Budget  HIGHSCH ENGLISH 502
7   Budget  HIGHSCH ENGLISH 650
1   Actual  COLL    ENGLISH 700
2   Actual  COLL    ENGLISH 540
3   Actual  COLL    ENGLISH 160
1   Budget  COLL    ENGLISH 820
2   Budget  COLL    ENGLISH 630
3   Budget  COLL    ENGLISH 800

我想要的是一个额外的列,其中包含每个FT,Group,Dept的累积量。

基本上,我希望它看起来像这样:

PERIOD  FT  GROUP   DEPT    VALUE   ACC VALUE   
1   Actual  KINDER  MATH    200 200 
2   Actual  KINDER  MATH    363 563 
3   Actual  KINDER  MATH    366 929 
1   Budget  KINDER  MATH    457 457 
2   Budget  KINDER  MATH    60  517 
3   Budget  KINDER  MATH    158 675 
1   Actual  HIGHSCH ENGLISH 456 456 
2   Actual  HIGHSCH ENGLISH 745 1201    
3   Actual  HIGHSCH ENGLISH 125 1326    
1   Budget  HIGHSCH ENGLISH 364 364 
2   Budget  HIGHSCH ENGLISH 158 522 
3   Budget  HIGHSCH ENGLISH 200 722 
1   Budget  HIGHSCH ENGLISH 502 502 
2   Budget  HIGHSCH ENGLISH 650 1152    
3   Budget  HIGHSCH ENGLISH 336 1488    
1   Actual  COLL    ENGLISH 700 700 
2   Actual  COLL    ENGLISH 540 1240    
3   Actual  COLL    ENGLISH 160 1400    
1   Budget  COLL    ENGLISH 820 820 
2   Budget  COLL    ENGLISH 630 1450    
3   Budget  COLL    ENGLISH 800 2250    

如果我在SQL 2012中,我会使用类似的东西:

SELECT   period
    ,ft
    ,group
    ,dept
    ,value
    ,CASE
        WHEN FT = 'Actual' THEN SUM(value) OVER (PARTITION BY dept, group, ft ORDER BY period)
        ELSE value
        END AS AccValue 
FROM myTable

但是,我在2008年并且不能使用这种方法,并且难以理解如何为我的数据复制它。

请有人帮忙吗?

2 个答案:

答案 0 :(得分:1)

这应该有效。 (我将禁止的列名从“GROUP”更改为“GROUP _”)

with cte (PERIOD,  FT,  GROUP_,   DEPT,   VALUE, AccValue) as (select t1.PERIOD,  t1.FT,  t1.GROUP_,   t1.DEPT,    t1.VALUE, SUM(t2.VALUE)   as AccValue
                            from            myTable t1
                            join myTable t2 on t1.PERIOD>= t2.PERIOD
                            and  t1.FT =  t2.FT 
                            and t1.GROUP_ = t2.GROUP_           
                            and t1.DEPT = t2.DEPT
                            group by    t1.PERIOD,  t1.FT,  t1.GROUP_,   t1.DEPT,    t1.VALUE
                    )
                                select  PERIOD
                    ,FT
                    ,GROUP_
                    ,DEPT
                    ,VALUE
                    ,CASE
                        WHEN FT = 'Actual' THEN AccValue
                        ELSE VALUE
                        END AS AccValue 
                        from cte
                                order by GROUP_ desc,  FT, PERIOD, DEPT desc,    VALUE

答案 1 :(得分:1)

简单的INNER JOIN应该可以解决问题。除非我误解了你,你想要的是一个总计,对吗?

此示例使用虚拟数据创建一个虚拟表,然后使用内部联接作为运行总计。从性能角度来看,公用表表达可能更有效。但为了简单起见,内部联接我是优先的。

/* Dummy table */    

create table testing1
(col1 int not null identity(1,1),
col2 varchar(5),
col3 int)


insert into testing1
values ('a', 10), ('a', 20), ('a', 30), ('b', 40), ('b', 50)

/* Running total example */

SELECT a.col1
           , a.col2
           , a.col3
           , SUM(b.col3) AS total

FROM testing1 a INNER JOIN testing1 b
     ON  a.col1 >= b.col1
     AND a.col2 = b.col2

GROUP BY a.col1, a.col2, a.col3
ORDER BY a.col1



/* Edit to include Output */
col1    col2    col3    total
1   a   10  10
2   a   20  30
3   a   30  60
4   b   40  40
5   b   50  90