过去12个月的总和

huangapple go评论53阅读模式
英文:

Sum of last 12 months

问题

我有一个包含3列(年份、月份、数值)的表格,类似于 Sql Server 中的这样:

年份 月份 数值 过去十二个月的值
2021 1 30 30
2021 2 24 54 (30 + 24)
2021 5 26 80 (54+26)
2021 11 12 92 (80+12)
2022 1 25 87 (从 2022 年 1 月到 2021 年 2 月的值之和)
2022 2 40 103 (从 2022 年 2 月到 2021 年 3 月的值之和)
2022 4 20 123 (从 2022 年 4 月到 2021 年 5 月的值之和)

我需要一个 SQL 查询来计算过去十二个月的值。

SELECT Year,
        Month,
        Value,
        SUM(Value) OVER (PARTITION BY Year, Month)
FROM MyTable
英文:

I have a table with 3 columns (Year, Month, Value) like this in Sql Server :

Year Month Value ValueOfLastTwelveMonths
2021 1 30 30
2021 2 24 54 (30 + 24)
2021 5 26 80 (54+26)
2021 11 12 92 (80+12)
2022 1 25 87 (SUM of values from 1 2022 TO 2 2021)
2022 2 40 103 (SUM of values from 2 2022 TO 3 2021)
2022 4 20 123 (SUM of values from 4 2022 TO 5 2021)

I need a SQL request to calculate ValueOfLastTwelveMonths.

SELECT Year,
        Month,
        Value,
        SUM (Value) OVER (PARTITION BY Year, Month)
FROM MyTable

答案1

得分: 2

这将变得容易得多如果你为每个月份和年份都有一行数据,然后(如果需要)可以过滤掉NULL行。之所以更容易是因为这样你就知道需要回溯多少行:11。

如果你制作一个包含年份和月份的数据集,然后可以LEFT JOIN到你的数据,进行汇总,最后筛选数据:

SELECT *
INTO dbo.YourTable
FROM (VALUES(2021,1,30),
            (2021,2,24),
            (2021,5,26),
            (2021,11,12),
            (2022,1,25),
            (2022,2,40),
            (2022,4,20))V(Year,Month,Value);

GO
WITH YearMonth AS(
    SELECT YT.Year,
           V.Month
    FROM (SELECT DISTINCT Year
          FROM dbo.YourTable) YT
         CROSS APPLY (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12))V(Month)),
RunningTotal AS(
    SELECT YM.Year,
           YM.Month,
           YT.Value,
           SUM(YT.Value) OVER (ORDER BY YM.Year, YM.Month
                               ROWS BETWEEN 11 PRECEDING AND CURRENT ROW) AS Last12Months
    FROM YearMonth YM
         LEFT JOIN dbo.YourTable YT ON YM.Year = YT.Year
                                   AND YM.Month = YT.Month)
SELECT Year,
       Month,
       Value,
       Last12Months
FROM RunningTotal
WHERE Value IS NOT NULL;

GO
DROP TABLE dbo.YourTable;
英文:

This is much easier if you have a row for each month and year, and then (if needed) you can filter the NULL rows out. The reason it's easier is because then you know how many rows you need to look back at: 11.

If you make a dataset of the years and months, you can then LEFT JOIN to your data, aggregate, and then finally filter the data out:

SELECT *
INTO dbo.YourTable
FROM (VALUES(2021,1,30),
            (2021,2,24),
            (2021,5,26),
            (2021,11,12),
            (2022,1,25),
            (2022,2,40),
            (2022,4,20))V(Year,Month,Value);

GO
WITH YearMonth AS(
    SELECT YT.Year,
           V.Month
    FROM (SELECT DISTINCT Year
          FROM dbo.YourTable) YT
         CROSS APPLY (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12))V(Month)),
RunningTotal AS(
    SELECT YM.Year,
           YM.Month,
           YT.Value,
           SUM(YT.Value) OVER (ORDER BY YM.Year, YM.Month
                               ROWS BETWEEN 11 PRECEDING AND CURRENT ROW) AS Last12Months
    FROM YearMonth YM
         LEFT JOIN dbo.YourTable YT ON YM.Year = YT.Year
                                   AND YM.Month = YT.Month)
SELECT Year,
       Month,
       Value,
       Last12Months
FROM RunningTotal
WHERE Value IS NOT NULL;

GO
DROP TABLE dbo.YourTable;

huangapple
  • 本文由 发表于 2023年2月14日 22:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定