Question : SQL

I would like to update the 2nd row that currently has 0 with the average of 145 and 165 or the rows above and below any field that has a zero value.  I want to replace a zero value with the aveage of the fields above and below the zero field.
Currently I can only think to use a CURSOR and move up and down the table performing the math of
Field2 = (field1 + field3 ) * .5  The zero value can be spread throughout the column.
The table will only have 24 records as it is the total based upon hour of day.
Volume
145
0
165

Answer : SQL

That should have been

update tbl
set volume = (select avg(b.volume) from tbl b where b.hr=tbl.hr-1 or b.hr=tbl.hr+1)
where volume = 0

This is too simplistic however, but it may suit your needs. It is simplistic because:

If your first hour has 0, it will copy the value directly from the next hour since there is no data prior.
If you have two or more consecutive zeroes, you will end up with some low valued data.  See this example of before/after.

Hr / Before / after
0 / 10 / 10
1 / 0 / 5
2 / 0 / 0
3 / 0 / 15
4 / 30 / 30
5 / 10 / 10
6 / 0 / 15
7 / 20 / 20
Random Solutions  
 
programming4us programming4us