Sql server 如何按行分组获取行的总和

Sql server 如何按行分组获取行的总和,sql-server,Sql Server,上面的查询正在抛出错误。有语法错误吗?请帮忙 我在执行查询时遇到此错误: ;With T(days,currentvalue) AS ( SELECT a.days,sum(counts) as currentvalue from SUMOP A group by days ) Update SUMOP set sumop.currentvalue=T.currentvalue where sumop.days=T.days 用在前面加一个分号,用join更新 The multi-par

上面的查询正在抛出错误。有语法错误吗?请帮忙

我在执行查询时遇到此错误:

;With T(days,currentvalue)  
AS
(
SELECT a.days,sum(counts) as currentvalue from SUMOP A
group by days
)
Update SUMOP set sumop.currentvalue=T.currentvalue
where sumop.days=T.days 

用在
前面加一个分号,用join更新

The multi-part identifier "T.days" could not be bound.

您的CTE T创建了一个行集,因此您需要从联接进行更新。

您可以使用窗口函数计算每天的
计数之和:

; With T(days,currentvalue)  
AS
(
SELECT a.days,sum(counts) as currentvalue from SUMOP A
group by days
)
Update SUMOP set sumop.currentvalue=T.currentvalue
FROM SUMOP, T where sumop.days=T.days 

如果显示错误,则会有帮助。无法绑定多部分标识符“T.days”。@user3540150已编辑,现在再试一次
update  t
set     currentvalue = t.curval
from    (
        select  currentvalue
        ,       sum(counts) over (partition by days) as curval
        from    sumop
        ) t