Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculating area under a curve - trapezium rule

Dan Scott GuestJul 6 2011 — edited Jul 7 2011
Hi,

I'm trying to calculate the 'area under the curve' using the trapezium rule for a set of data points. i.e. I have a set of values, sampled at irregular intervals.
create table measurements (
  measurement_value integer,
  measurement_time timestamp
);

insert into measurements (measurement_value,measurement_time) values (50,TIMESTAMP'2011-01-01 00:00:00');
insert into measurements (measurement_value,measurement_time) values (40,TIMESTAMP'2011-01-01 01:00:00');
insert into measurements (measurement_value,measurement_time) values (40,TIMESTAMP'2011-01-01 03:00:00');
insert into measurements (measurement_value,measurement_time) values (50,TIMESTAMP'2011-01-01 03:30:00');
insert into measurements (measurement_value,measurement_time) values (40,TIMESTAMP'2011-01-01 05:00:00');
insert into measurements (measurement_value,measurement_time) values (40,TIMESTAMP'2011-01-01 06:00:00');
insert into measurements (measurement_value,measurement_time) values (60,TIMESTAMP'2011-01-01 07:00:00');
I'd like to calculate the area of the 'curve' where the value was below a threshold of, e.g. 45.

I can use the trapezium rule and assume a straight line between subsequent measurement, i.e. for the first 2 data points, the value drops from 50 to 40 linearly over the hour, and so the area for those measurements is (30min * 5)/2. For the 2nd and 3rd data points, the area is (120min * 5), for the 3rd and 4th, the area is (15min * 5)/2. And so on. The values are not necessarily round numbers, almost any value is possible.

I've started by using 'lead' to obtain the subsequent measurement value and time, but I'm getting stuck dealing with the end points and non-trivial interpolation. Does anyone have any pointers to get me going?

Thanks,

Dan Scott
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2011
Added on Jul 6 2011
3 comments
543 views