|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi experts,
you are nearly my last chance to solve a sql problem. I hope that someone can help me. Keywords: average, time offset, data period I want to get data from a database in Excel to calculate a mathematic model. The problem is, that the model requires averaged data. I have 26 variables, which are stored every 10 seconds. What I would like to get is a sliding average value with 5 minutes before and five minutes after a specific time. +---------------------+---------+------------- | DateTime | Value 1 | Avg(Value1) |---------------------+---------+------------- |3/14/2003 2:55:00 PM | 856 | 850 |3/14/2003 2:56:00 PM | 850 | 851 |3/14/2003 2:57:00 PM | 852 | 851 |3/14/2003 2:58:00 PM | 600 | 850 |3/14/2003 2:59:00 PM | 860 | 849 |3/14/2003 3:00:00 PM | 854 | 850 |3/14/2003 3:01:00 PM | 850 | 850 |3/14/2003 3:02:00 PM | 849 | 849 |3/14/2003 3:03:00 PM | 865 | 850 |3/14/2003 3:04:00 PM | 851 | 850 |3/14/2003 3:05:00 PM | 849 | 850 |3/14/2003 3:06:00 PM | 855 | 851 What I have now is: SELECT CONVERT(VARCHAR(10), DateTime, 110) + ' ' + CONVERT(VARCHAR(2), DateTime, 108) + ':' + CONVERT(VARCHAR(2), FLOOR(DATEPART(mi, DateTime) / 5) * 5) AS 'Five Minute Period Starting', TagName, AVG(Value) AS 'Average' FROM Runtime..AnalogHistory WHERE TagName IN ('Variable1', 'Varable2', 'Variable3') AND DateTime >= '3/14/2003 2:55' AND DateTime < '3/17/2003 11:00' AND wwRetrievalMode = 'DELTA' GROUP BY CONVERT(VARCHAR(10), DateTime, 110) + ' ' + CONVERT(VARCHAR(2), DateTime, 108) + ':' + CONVERT(VARCHAR(2), FLOOR(DATEPART(mi, DateTime) / 5) * 5), TagName With this code I can get the average from the values in a five minute data period. This code doesn’t calculate a sliding average. But I need a sliding average to set a time offset or to get data period from, for example, an average value every two minutes. I’m not sure if I’m describing my problem clearly. Thanks a lot for your help in advance. Best regards Melanie |
|
#2
|
|||
|
|||
|
You will need something similar to this
Select DateTime, Value, (Select Avg(Value) from Table1 Where Table1.DateTime between (t.DateTime +5 min) and t.DateTime - 5 min)) as 'Average' From Table1 as T S- |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > How to get a sliding average value for periodic stored values. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|