TSQL-2008 SUM(X) OVER (PARTITION … ORDER BY CLAUSE)

折月煮酒 提交于 2019-12-02 13:28:47

问题


I really need to take what I have as a result of a CTE, and calculate the cummulative value of groups of data.

The dataset is:

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

What I want is an additional column that holds the cummulative amount for each FT, Group, Dept.

So basically, I would like it to look like this:

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    

If I was in SQL 2012, I'd use something similar to this:

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

However, I am on 2008 and can't use this method and am stumped on how to replicate this for my data.

Please can anyone help?


回答1:


This should work. (I changed a forbidden column name from "GROUP" to "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



回答2:


Simple INNER JOIN should do the trick. Unless I'm misunderstanding you, what you want is a running total, correct?

This example creates a dummy table with dummy data, then uses an inner join for the running total. From a performance standpoint, the Common Table Expression is likely more efficient. But for simplicity, the inner join my be preferential.

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


来源:https://stackoverflow.com/questions/19684249/tsql-2008-sumx-over-partition-order-by-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!