I have this table Test as Id, SomeValue, SomeText contains about a mill records, I altered the table to
ALTER TABLE Test
ADD ComputedColFloat FLOAT
all are set to null, fine! what I want is that for each row, the value of ComputedColFloat should be set to sum of values in column SomeValue upto that row. (The values in SomeValue are computed using RAND()!)
Say, if before adding the column, first five rows are
Id SomeValue SomeText
1 .91 text
2 .34 text
3 .52 text
4 .11 text
5 .68 text
what I want is to write a query that would update the table and set the added column to reflect the values as
Id SomeValue SomeText ComputedColFloat
1 .91 text .91 // .91
2 .34 text 1.25 // .91 + .34
3 .52 text 1.77 // .91 + .34 + .52
4 .11 text 1.88 // .91 + .34 + .52 + .11
5 .68 text 2.56 // .91 + .34 + .52 + .11 + .68
I am using sql server 2008, and would prefer to do this from SSMS not from visual studio by C# or anything! (In C# in VS, I could loop, find the sum and then write a query that would update the records, but that’s not what I need, I need to do it from SSMS)
Also, if there are more than 1 way to do this, I would like to know what would be best performance wise, as I have about a mill records, I don’t wanna wait up forever! 🙁
You can create cursor for your table and update each row.
Before executing take a copy of your table to verify