I've got the following problem that I would like to cope with - I have a SQL dataset that I would like to section (e.g. like this one):
OldTimetag OldValue
2012-05-03 12:47:00 5
2012-05-03 13:00:00 1.3
2012-05-03 13:21:00 7
2012-05-03 14:56:00 5
2012-05-03 14:57:00 0.3
.... ....
Now, I want to section (and/or average) the data based on a user-chosen interval - into new timetags, e.g. every 15 minutes with the first timetag as starting point, i.e.:
NewTimetag NewValue
2012-05-03 12:47:00 4.507
2012-05-03 13:02:00 1.3
.... ....
The main constraint is that the value next to the timetag is always valid, until the next timetag appears. So the value of 5 at timetag 2012-05-03 12:47:00 is valid for the next 13 minutes until 13:00:00. The value for the first 15 minutes from 12:47:00 would be (13*5+2*1.3)/15 = 4.507. In the next 15 minutes, at 13:02:00 the value is simply equal to 1.3... (and so on)
I've come so long, that it is a good idea to make an "artificial table" first, to later join it with the old table. I'm generating that table by:
DECLARE @intStart datetime, @intEnd datetime
SELECT @intStart =min(OldTimetag), @intEnd = MAX(OldTimetag)
FROM OldTable
where OldTimetag between '2012-05-03 12:47:00' and '2012-05-03 14:57:00'
Declare @ArtificalTable table (NewTimeTag datetime, NewValue Float)
Declare @MinuteSlicer Int
Set @MinuteSlicer = 15
Insert @Hallo Select @intStart, null
While ( @intStart < @intEnd ) BEGIN
Insert @ArtificalTable
Select DATEADD(mi,@MinuteSlicer, @intStart), Null
Set @intStart = DATEADD(mi,@MinuteSlicer,@intStart)
If @intEnd <= DATEADD(mi,@MinuteSlicer,@intStart)
Break
End
This gives me an output like:
NewTimetag NewValue
2012-05-03 12:47:00 Null
2012-05-03 13:02:00 Null
.... ....
However, I'm having problems with the next step, how to join the tables correctly - can anyone give me a hint?