累计总数字段

时间:2019-08-06 06:41:44

标签: sql-server tsql

我有一个看起来像这样的数据集:

Key       TypeSeq   Type    Duration
-------------------------------------
29671461    10      1001       4
29671461    20      1002       2
29671461    30      1003       0
29671461    40      1004       0
29671461    70      1007     261
29671463    10      1001       3
29671463    20      1002       5
29671463    30      1003       7
29671463    40      1004       8
29671463    70      1007     261

我找到了这个,但是我想按ID而不是按其分组

select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id

我需要第5列,该列按关键列进行累加总计

1 个答案:

答案 0 :(得分:0)

您可以将sum()用作窗口功能。如果将order byselect "Key", TypeSeq, type, duration, sum(duration) over (partition by "Key" order by TypeSeq) as sum from the_table order by "Key", TypeSeq; 一起使用,您将获得一个累积的总和:

IEntityTypeConfiguration