Given a table
ID FRUIT
-- -----
1 APPLE
2 BANANA
3 PEAR
4 APPLE
5 APPLE
6 PEAR
I want to get this
ID FRUIT RUNNING_TOTAL
-- ----- -------------
1 APPLE 1
2 BANANA 1
3 PEAR 1
4 APPLE 2
5 APPLE 3
6 PEAR 2
(going in the ID order, the first time we encounter a given fruit we set the value of the RUNNIN_TOTAL for that row to 1; the second time we encounter a given fruit, RUNNIN_TOTAL is 2, and so on).
I think I need to first add a column like this:
alter table Fruits add RUNNING_TOTAL int null
Then set the values for the new column with some thing like this
update Fruits set RUNNING_TOTAL = ...
but I am not sure how to complete the last statement. Can someone help? I am using SQL SERVER 2008, but a portable solution would be ideal.
Thanks!
MySQL,Postgres,sql server 2008, etc?