I use Oracle and have the following table:
create table test as
select to_date('02.05.2017 00:00', 'DD.MM.YYYY HH24:MI') as DT, 203.4 as VAL from dual union all
select to_date('02.05.2017 01:00', 'DD.MM.YYYY HH24:MI') as DT, 206.7 as VAL from dual union all
select to_date('02.05.2017 02:00', 'DD.MM.YYYY HH24:MI') as DT, 208.9 as VAL from dual union all
select to_date('02.05.2017 03:00', 'DD.MM.YYYY HH24:MI') as DT, 211.8 as VAL from dual union all
select to_date('02.05.2017 04:45', 'DD.MM.YYYY HH24:MI') as DT, 212.3 as VAL from dual union all
select to_date('02.05.2017 06:15', 'DD.MM.YYYY HH24:MI') as DT, 214.5 as VAL from dual union all
select to_date('02.05.2017 08:12', 'DD.MM.YYYY HH24:MI') as DT, 215 as VAL from dual
;
DT VAL
----------------------------
02.05.2017 00:00 203.4
02.05.2017 01:00 206.7
02.05.2017 02:00 208.9
02.05.2017 03:00 211.8
02.05.2017 04:45 212.3
02.05.2017 06:15 214.5
02.05.2017 08:12 215
I need to write SQL query (or PL/SQL procedure) so as to interpolate the value (VAL) for any timestamp (DT), assuming that value is increasing constantly between two neighbour records in table (ie. linear interpolation).
Example:
When I select value for timestamp '02.05.2017 00:00', query should give me 203.4 (record with such timestamp exists in table)
When I select value for timestamp '02.05.2017 00:30', query should give me 205.05 (record with such timestamp doesn't exist in table, so we take a 'middle' between 203.4 and 206.7, because wanted timestamp is in the middle between their timestamps)
When I select value for timestamp '02.05.2017 00:15', query should give me 204.225 (a 'fourth part' between 203.4 and 206.7)
What is the simplest way to solve such task?