如何按季度总结 [英] how to sum on quarterly basis

查看:57
本文介绍了如何按季度总结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储过程总计第三个计数,早期值最多为第三个计数





A stored procedure to sum on the third count, the earlier values up to the third count

.

counter values sum-value-quarterly
----------------------------------------------
1        10
2        14
3        16             40
4        20 
5        30
6        50             100 
7        25 
8        35
9        10              70
10       15
11       25



你如何设定程序保存上述条件









计数器最多可以说24 ..


How do you set the condition for the program to carry up the above




The counter is say up to 24..

推荐答案

检查这种方法:

Check this approach:
select t1.value, t1.counter,
CASE WHEN t1.counter % 3 = 0 THEN (select sum(t2.value) from test t2 where (t2.counter-1) / 3 = (t1.counter-1) / 3)
ELSE NULL END as [sum-value-quarterly]
from test t1


将内部SELECT UNION替换为您的表名。

Replace Inner SELECT UNION with your table name.
SELECT
[QGroup], SUM([values]) AS [sum-value-quarterly]
FROM
(
    SELECT *, CEILING([counter] /3.0) AS [QGroup]
    FROM
    (
    SELECT 1 AS [counter],10 AS [values] 
    UNION
    SELECT 2 AS [counter],14 AS [values] 
    UNION
    SELECT 3 AS [counter],16 AS [values] 
    UNION
    SELECT 4 AS [counter],20 AS [values] 
    UNION
    SELECT 5 AS [counter],30 AS [values] 
    UNION
    SELECT 6 AS [counter],50 AS [values] 
    UNION
    SELECT 7 AS [counter],25 AS [values] 
    UNION
    SELECT 8 AS [counter],35 AS [values] 
    UNION
    SELECT 9 AS [counter],10 AS [values] 
    UNION
    SELECT 10 AS [counter],15 AS [values] 
    UNION
    SELECT 11 AS [counter],25 AS [values]
    )AS X
)AS Y
GROUP BY [QGroup]
ORDER BY [QGroup]


这篇关于如何按季度总结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆