简体   繁体   English

将一列的三个值求和并将其插入SQL Server 2008 R2中的另一列

[英]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.

相关问题 SQL SERVER 2008 R2在另一列中查找列词 - SQL SERVER 2008 R2 find column words in another column SQL Server 2008 R2,为另一列的每个不同值选择一个列的一个值 - SQL server 2008 R2, select one value of a column for each distinct value of another column 在使用SQL Server 2008 R2查询时如何使用其他列替换提取的值 - How to replace fetched values with another column while querying with SQL Server 2008 R2 根据两个列值是否出现在SQL Server 2008 R2的另一个表中来更新表 - Update a table based on if two column values appear in another table on SQL Server 2008 R2 如何在SQL Server 2008 R2中添加具有其他列值作为默认值的列? - How to add a column which has another column value as default value in SQL Server 2008 R2? 将一列从一个表复制到SQL Server 2008 R2中的另一个表 - Copy a column from a table to another table in SQL Server 2008 R2 根据另一列的最大值选择一个不同的行SQL Server 2008 R2 - Select a distinct row based on the max value of another column SQL Server 2008 R2 通过另一个表中的列更新SQL Server 2008 R2中的表,并且还需要求和值 - Update a table in SQL Server 2008 R2 by a column in another table and also need a sum value 连接两个Float列,并将它们插入到SQL Server 2008 R2中的另一个Float列中 - Concatenating two Float columns and insert them to another Float column in SQL Server 2008 R2 加密SQL Server 2008 r2中现有表的列 - Encrypt column of existing tables in SQL Server 2008 r2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM