[英]Summing three values of one column and inserting it in another column in SQL Server 2008 R2
I have a table like this : 我有这样一张桌子:
(ID)---(count)----(SumCount)
I have three rows (three count) for each ID, I want to sum them and insert them in sumCount
grouped by their IDs. 每个ID我都有三行(三计数),我想对它们求和并将其插入按ID分组的sumCount
。
My ideal result is one row for each ID and the SumCount for each of them. 我理想的结果是每个ID一行,每个ID的SumCount。
Do I have to join the table to itself? 我是否必须将桌子自己连接起来?
Update Mytable
set sumCount = (select sum(count) as SumCount from Mytable group by ID)
The problem is: it does not gives just one value and I get the error 问题是:它不仅给出一个值,而且出现错误
Update Yourtable
set sumCount=z.SumCount
From (select ID, sum(count) as SumCount
from Yourtable
groub by ID
)z
where YourTable.Id = Z.ID
i think this worked correctly. 我认为这工作正常。
try and response 尝试并回应
Update Mytable M1
set sumCount=(select sum(count) from Mytable M2 WHERE M1.ID=M2.ID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.