Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Time Averaging

I have a table in SQL Server that stores statistics for a piece of hardware, rows in the table represent data for a given second. It contains for example, these columns:

timestamp (DateTime)
value (int)

What I want to do is select the data from the table for a given date/time range but return it in such a way that it averages for a given time period (such as 1 minute, 5 minute, 1 day etc) between the given range. So for an hour I'd have 60 rows of 1 minute averages.

Where do I start with this? Anybody any points or ideas?

like image 564
Lloyd Avatar asked Jun 16 '09 11:06

Lloyd


People also ask

How do you calculate average time in SQL query?

To calculate this average, you can use AVG command. – Averages per minute: to obtain the averages per minute, you must retrieve E3TimeStamp's seconds and milliseconds. To do so, multiply this field by 24 (to convert the time base into hours), and then by 60 (to convert it into minutes).

How do you calculate 3 day moving average in SQL?

To do so, we calculate the average of the stock prices from three consecutive days—the day in question and the two previous days—then repeat the same for each day in the data set. This is a three-day moving average, because we average over a period of three days.

How do you find the average between two dates in SQL?

Answer: To find the average time between two dates, you could try the following: SELECT TO_DATE(date1, 'yyyy/mm/dd') + ((TO_DATE(date2, 'yyyy/mm/dd') - TO_DATE(date1, 'yyyy/mm/dd')) /2 ) FROM dual; This will calculate the elapsed time between date1 and date2.

How do you find the average value of time?

Drawing a line between each value in the sample (including the interpolated values) Calculating the area below the line. Dividing the result of the area calculation by the duration of the sample. The result of this step is the time average for the sample.


2 Answers

You can do a select and group by on a DatePart of your timestamp.

For Example:

SELECT
    DATEPART(hh, [timestamp]),
    DATEPART(mi, [timestamp]),
    AVG([value])
FROM
    YourTable
WHERE
    [timestamp] BETWEEN '2009-01-01 00:00:00.000' AND '2009-02-01 00:00:00.000'
GROUP BY
    DATEPART(hh, [timestamp]),
    DATEPART(mi, [timestamp])

EDIT: For your more complex time spans like 5 mins, you can do a divide on the datepart as follows.

DATEPART(mi, [timestamp]) / 5 * 5
like image 194
Robin Day Avatar answered Jan 10 '23 13:01

Robin Day


WITH    cal(m) AS
        (
        SELECT  1
        UNION ALL
        SELECT  m + 1
        FROM    cal
        WHERE   m < 60
        )
SELECT  DATEADD(minute, m, @start), AVG(value)
FROM    cal
LEFT JOIN
        timestamp
ON      timestamp > DATEADD(minute, m, @start)
        AND timestamp <= DATEADD(minute, m + 1, @start)
GROUP BY
        m

This will select averages for all minutes inside a given hour, even those for which there are no records.

like image 45
Quassnoi Avatar answered Jan 10 '23 13:01

Quassnoi