tags:

views:

27

answers:

0

I have a table with:

  • location_id (location) - ranges from 0-90,000
  • time (time_period) - ranges from 1-15 for each location
  • temperature (temp) - A unique value for each location x time_period.

Example data:

location time_period temp
91         1          -4
91         2          3
91         3          12
.......................
.......................
91         15         20

I'd like to create a new field called cum_temp and add the cumulative value for each cell till that current time_period. My current thought is to do duplicate the table and run:

update site_a 
   set cum_temp = (select sum(temp) 
                     from site_a_copy 
                    where site_a_copy.location = site_a.location 
                      and site_a_copy.time_period <= site_a.time_period);

Is this the most efficient way to do this or can you suggest something better?