缺少日期的总和

时间:2020-02-21 12:24:20

标签: sql sql-server date running-total

我有一个这样的数据表:

bash: -c: line 0: conditional binary operator expected
bash: -c: line 0: syntax error near `filename'
bash: -c: line 0: `[[  =~ filename ]] && git ls-files --deleted >! deleted_files.txt'

在最后一栏中,我计算了每个部件号和每个Reg的7个月连续清除量。为此,我编写了以下代码:

PeriodYearMonth Reg      PartNo             ComponentRemovals   RunningRemovals
2019 10         G-NHVP   109-0740V01-137    1                    1
2019 11         G-NHVP   109-0740V01-137    1                    2  
2019 12         G-NHVP   109-0740V01-137    1                    3
2020 01         G-NHVP   109-0740V01-137    1                    4
2019 10         OO-NSF   11-13354P          1                    1
2019 09         G-NHVR   11-13354P          2                    2 
2019 10         OY-HMV   11-13354P          1                    1

但是,由于并非所有月份都包含在PeriodYearMonth列中,因此结果不正确。我已经在网上看到了一些简单案例的解决方案,但对我来说,棘手的是,我需要每个PartNo,每个Reg和每个月都有一个条目。

任何帮助将不胜感激。

致谢

2 个答案:

答案 0 :(得分:1)

要解决此问题,您需要创建所有$whusers = "username1","username2","username3" Foreach ($user in $whusers) { xcopy "C:\users\myname\desktop\shortcut.url" "F:\corp users\$user\desktop" } 值(使用递归CTE生成)以及不同的PeriodYearMonthReg对以及{{1 }}互相获取列的所有组合。然后,可以将此合并表PartNo与原始表进行比较,以获取CROSS JOINLEFT JOINComponentRemovals中的每个Reg,然后将它们相加使用窗口功能:

PartNo

输出

PeriodYearMonth

Demo on SQLFiddle

请注意,如果WITH CTE AS ( SELECT CONVERT(DATE, CONCAT(REPLACE(MIN(PeriodYearMonth), ' ', '-'), '-01'), 23) AS date, CONVERT(DATE, CONCAT(REPLACE(MAX(PeriodYearMonth), ' ', '-'), '-01'), 23) AS max_date FROM vtRelRepComponentsRemovalsByPartNo UNION ALL SELECT DATEADD(MONTH, 1, date), max_date FROM CTE WHERE date < max_date ) SELECT FORMAT(CTE.date, 'yyyy MM') AS PeriodYearMonth , rp.Reg , rp.PartNo , COALESCE(v.ComponentRemovals, 0) AS ComponentRemovals , SUM(COALESCE(v.ComponentRemovals, 0)) OVER (PARTITION BY rp.Reg, rp.PartNo ORDER BY FORMAT(CTE.date, 'yyyy MM') ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS RunningRemovals FROM CTE CROSS JOIN (SELECT DISTINCT Reg, PartNo FROM vtRelRepComponentsRemovalsByPartNo) rp LEFT JOIN vtRelRepComponentsRemovalsByPartNo v ON v.PeriodYearMonth = FORMAT(CTE.date, 'yyyy MM') AND v.Reg = rp.Reg AND v.PartNo = rp.PartNo ORDER BY rp.Reg, rp.PartNo, CTE.date 中存在感兴趣的所有 PeriodYearMonth Reg PartNo ComponentRemovals RunningRemovals 2019 09 G-NHVP 109-0740V01-137 0 0 2019 10 G-NHVP 109-0740V01-137 1 1 2019 11 G-NHVP 109-0740V01-137 1 2 2019 12 G-NHVP 109-0740V01-137 1 3 2020 01 G-NHVP 109-0740V01-137 1 4 2019 09 G-NHVR 11-13354P 2 2 2019 10 G-NHVR 11-13354P 0 2 2019 11 G-NHVR 11-13354P 0 2 2019 12 G-NHVR 11-13354P 0 2 2020 01 G-NHVR 11-13354P 0 2 2019 09 OO-NSF 11-13354P 0 0 2019 10 OO-NSF 11-13354P 1 1 2019 11 OO-NSF 11-13354P 0 1 2019 12 OO-NSF 11-13354P 0 1 2020 01 OO-NSF 11-13354P 0 1 2019 09 OY-HMV 11-13354P 0 0 2019 10 OY-HMV 11-13354P 1 1 2019 11 OY-HMV 11-13354P 0 1 2019 12 OY-HMV 11-13354P 0 1 2020 01 OY-HMV 11-13354P 0 1 个值,则可以简单地使用PeriodYearMonth子查询来获取这些值,而不是递归{ {1}}例如

vtRelRepComponentsRemovalsByPartNo

Demo on SQLFiddle

答案 1 :(得分:0)

在最后一列中,我计算了过去7个月每个部件号和每个Reg的去除量的总和。

删除windowing子句和过滤器:

select [PeriodYearMonth], [Reg], [PartNo], [ComponentRemovals],
        sum(sum(ComponentRemovals)) over (Partition by [Reg], PartNo Order By PeriodYearMonth, PeriodYearMonth) as RunningRemovals
        [ConfirmedFailures],
        sum(sum(ConfirmedFailures)) over (Partition by [Reg], PartNo Order By PeriodYearMonth, PeriodYearMont) as RunningFailures
from [RALNHVTST].[dbo].[vtRelRepComponentsRemovalsByPartNo]
where PeriodYearMonth >= format(dateadd(month, -7, getdate()), 'yyyy MM')
group by Reg, PartNo, ComponentRemovals, ConfirmedFailures, PeriodYearMonth
Order by PartNo