Sql server 滚动6个月

Sql server 滚动6个月,sql-server,Sql Server,我有一个存储过程的一部分,它返回的数据与下面的数据类似 -- Note: I use this parameter to determine the the last of the 6 reporting periods to display DECLARE @endMonth CHAR(6) = '202009' CREATE TABLE Table1 ( [company] varchar(9), [starting_period] int, [reportin

我有一个存储过程的一部分,它返回的数据与下面的数据类似

-- Note: I use this parameter to determine the the last of the 6 reporting periods to display
DECLARE @endMonth CHAR(6) = '202009'

CREATE TABLE Table1
(
    [company] varchar(9), 
    [starting_period] int, 
    [reporting_period] int, 
    [gross] numeric
);
    
INSERT INTO Table1
    ([company], [starting_period], [reporting_period], [gross])
VALUES
    ('company_1', 201911, 202003, 2354.36),
    ('company_1', 201912, 202003, 290),
    ('company_1', 202001, 202003, 4299.98),
    ('company_1', 202002, 202003, 2997.96),
    ('company_1', 201911, 202004, 1644.36),
    ('company_1', 201912, 202004, 240),
    ('company_1', 202001, 202004, 7332.43),
    ('company_1', 202002, 202004, 1429.57),
    ('company_1', 202004, 202004, 1511.25),
    ('company_1', 201911, 202005, 1519.36),
    ('company_1', 201912, 202005, 700),
    ('company_1', 202001, 202005, 1478.87),
    ('company_1', 202002, 202005, 2314),
    ('company_1', 202004, 202005, 5687),
    ('company_1', 201911, 202006, 1519.36),
    ('company_1', 201912, 202006, 100),
    ('company_1', 202001, 202006, 871.4),
    ('company_1', 202002, 202006, 317.51),
    ('company_1', 202004, 202006, 2359.47),
    ('company_1', 201911, 202007, 1519.36),
    ('company_1', 201912, 202007, 150),
    ('company_1', 202001, 202007, 871.4),
    ('company_1', 202002, 202007, 429.34),
    ('company_1', 202004, 202007, 1725.73),
    ('company_1', 201911, 202008, 1519.36),
    ('company_1', 201912, 202008, 150),
    ('company_1', 202001, 202008, 654.97),
    ('company_1', 202002, 202008, 453.34),
    ('company_1', 202004, 202008, 3540.29),
    ('company_1', 201911, 202009, 1444.36),
    ('company_1', 201912, 202009, 150),
    ('company_1', 202001, 202009, 254.97),
    ('company_1', 202002, 202009, 403.34),
    ('company_1', 202004, 202009, 2192.37),
    ('company_2', 201911, 202003, 13530.76),
    ('company_2', 201912, 202003, 555.25),
    ('company_2', 202001, 202003, 12700.03),
    ('company_2', 202002, 202003, 62050),
    ('company_2', 201911, 202004, -10522.76),
    ('company_2', 201912, 202004, 230.25),
    ('company_2', 202001, 202004, 8374.63),
    ('company_2', 202002, 202004, 7412.75),
    ('company_2', 202004, 202004, 17034.51),
    ('company_2', 201911, 202005, 1173),
    ('company_2', 201912, 202005, 230.25),
    ('company_2', 202001, 202005, 1608.55),
    ('company_2', 202002, 202005, 7620.55),
    ('company_2', 202004, 202005, 17640.31),
    ('company_2', 202005, 202005, 1242.93),
    ('company_2', 201911, 202006, 1173),
    ('company_2', 201912, 202006, 280.25),
    ('company_2', 202001, 202006, 478.55),
    ('company_2', 202002, 202006, 371.66),
    ('company_2', 202004, 202006, 7972.67),
    ('company_2', 202005, 202006, 651.93),
    ('company_2', 202006, 202006, 44693.61),
    ('company_2', 201911, 202007, 1534),
    ('company_2', 201912, 202007, 380.88),
    ('company_2', 202001, 202007, 305.46),
    ('company_2', 202002, 202007, 421.66),
    ('company_2', 202004, 202007, 4053.74),
    ('company_2', 202005, 202007, 1552.93),
    ('company_2', 202006, 202007, 20787.43),
    ('company_2', 202007, 202007, 200),
    ('company_2', 201911, 202008, 1173),
    ('company_2', 201912, 202008, 280.25),
    ('company_2', 202001, 202008, 567.62),
    ('company_2', 202002, 202008, 371.66),
    ('company_2', 202004, 202008, 1235),
    ('company_2', 202005, 202008, 8168.53),
    ('company_2', 202006, 202008, 22617.11),
    ('company_2', 202007, 202008, 305),
    ('company_2', 201911, 202009, 1173),
    ('company_2', 201912, 202009, 280.25),
    ('company_2', 202001, 202009, 817.62),
    ('company_2', 202002, 202009, 421.66),
    ('company_2', 202004, 202009, 7473.81),
    ('company_2', 202005, 202009, 901.93),
    ('company_2', 202006, 202009, 7863.36),
    ('company_2', 202007, 202009, 280),
    ('company_2', 202008, 202009, 219.66),
    ('company_3', 202005, 202005, 2497.62),
    ('company_3', 202005, 202006, 5754.51),
    ('company_3', 202006, 202006, 15944.31),
    ('company_3', 202005, 202007, 6857.47),
    ('company_3', 202006, 202007, 19114.26),
    ('company_3', 202007, 202007, 374.97),
    ('company_3', 202005, 202008, 1052.96),
    ('company_3', 202006, 202008, 28627.23),
    ('company_3', 202007, 202008, 349.5),
    ('company_3', 202008, 202008, 5266.82),
    ('company_3', 202005, 202009, 1638.07),
    ('company_3', 202006, 202009, 4023.55),
    ('company_3', 202007, 202009, 325),
    ('company_3', 202008, 202009, 943.05);
我的预期结果如下,我希望这也能突出我的解释

-- Expected Output:
+-----------+------------------+-----------+
|           | reporting_period | gross_sum |
+-----------+------------------+-----------+
| company_1 | 202008           | 14823.74  |
| company_2 | 202008           | 148155.7  |
| company_3 | 202008           | 85839.65  |
| company_1 | 202009           | 17016.11  |
| company_2 | 202009           | 164894.5  |
| company_3 | 202009           | 92769.32  |
+-----------+------------------+-----------+
我希望通过
公司
报告期
来汇总这些信息。我想通过创建滚动总额来显示每个报告期内每个公司的
总额
。每个报告期应包含报告期的0至5个月的总金额。我相信开始时间应该在同一范围内

例如,对于
报告期
202009
公司_1
SUM(gross)
用于以下记录

+-----------+-----------------+------------------+---------+
| company   | starting_period | reporting_period | gross   |
+-----------+-----------------+------------------+---------+
| company_1 | 202004          | 202004           | 1511.25 |
| company_1 | 202004          | 202005           | 5687    |
| company_1 | 202004          | 202006           | 2359.47 |
| company_1 | 202004          | 202007           | 1725.73 |
| company_1 | 202004          | 202008           | 3540.29 |
| company_1 | 202004          | 202009           | 2192.37 |
+-----------+-----------------+------------------+---------+
-- Total Gross: 17016.11
我试着使用我以前的帖子,但没有成功。我还尝试了一个相当混乱的解决方案,通过
SUM(CASE-WHEN…THEN)
,但我也没有成功,我更希望得到预期的输出。我遗漏了一些东西,但我一直无法找出哪里出了问题。提前感谢你的帮助

编辑:

利用这个代码块,总结果远远大于预期结果。如果我按公司删除
分区
,也会发生同样的情况。自上一个post示例以来,数据略有变化

我在这组文章中加入了上一篇文章中的日历表,因为我仍然无法获得正确的总和,这可能是因为我制作的玩具数据与我的实际数据不尽匹配。此后,我添加了
公司
起始期
,并加入了日历表以匹配提供的示例集。我还按照建议扩展了日历表。我以为我能够根据我的实际数据推断出先前的结果,但我还没有成功地得到预期的结果

SELECT t.company
       -- ,t.starting_period
       ,t.reporting_period
       ,SUM(SUM(t.gross)) OVER (-- PARTITION BY t.company
                                             ORDER BY t.reporting_period -- , t.starting_period
                                             ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
                                             ) rolling_6_months
FROM #test t
GROUP BY t.company
         -- ,t.starting_period
         ,t.reporting_period
我在excel中对总体数据进行了一些检查,可以得到预期的总和,但我很难编写SQL以生成所需的结果

SELECT t.company
       -- ,t.starting_period
       ,t.reporting_period
       ,SUM(SUM(t.gross)) OVER (-- PARTITION BY t.company
                                             ORDER BY t.reporting_period -- , t.starting_period
                                             ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
                                             ) rolling_6_months
FROM #test t
GROUP BY t.company
         -- ,t.starting_period
         ,t.reporting_period

您能告诉我们GMB解决方案有什么问题吗?@Squirrel感谢您查看我编辑的帖子,其中包含了一些以前的代码、附加说明,以及所用代码的某个报告期的输出