# AVG columns by time frame

**URL:** <https://forum.inductiveautomation.com/t/avg-columns-by-time-frame/52342>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [October 26, 2021, 1:00pm UTC](https://forum.inductiveautomation.com/t/avg-columns-by-time-frame/52342 "2021-10-26T13:00:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [October 26, 2021, 1:00pm UTC](https://forum.inductiveautomation.com/t/avg-columns-by-time-frame/52342/1 "2021-10-26T13:00:28Z")

</div>

Im using this query to AVG 2 columns:  
SELECT  
FORMAT(CONVERT(datetime,AVG(CONVERT(FLOAT,(“3A\_End\_Time” - “3A\_Start\_Time”)))), ‘HH:mm:ss’) AS AvgDuration  
FROM  
Misc\_Rail\_LGV\_Call\_Time  
How can I add a time frame into this, if I wanted the AVG the last 12hrs.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [October 26, 2021, 2:57pm UTC](https://forum.inductiveautomation.com/t/avg-columns-by-time-frame/52342/2 "2021-10-26T14:57:39Z")

</div>

You didn’t specify which DB you’re using, but either way you would need to add a WHERE clause to your query. In MySQL it would look something like this:

```sql
SELECT FORMAT(CONVERT(datetime,AVG(CONVERT(FLOAT,(“3A_End_Time” - A_Start_Time”)))), ‘HH:mm:ss’) AS AvgDuration
FROM Misc_Rail_LGV_Call_Time
WHERE t_stamp > DATE_SUB(NOW(), INTERVAL 12 HOUR))

```

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [October 26, 2021, 3:00pm UTC](https://forum.inductiveautomation.com/t/avg-columns-by-time-frame/52342/3 "2021-10-26T15:00:42Z")

</div>

Alright, I found out that this here works:

WHERE  
t\_stamp \>= DATEADD (HOUR, -12, getdate())
