简体   繁体   中英

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.

My ideal result is one row for each ID and the SumCount for each of them.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM