# DB Data calculation

**URL:** <https://forum.inductiveautomation.com/t/db-data-calculation/29272>\
**Category:** Ignition\
**Created:** [October 22, 2019, 12:27pm UTC](https://forum.inductiveautomation.com/t/db-data-calculation/29272 "2019-10-22T12:27:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IMEngineering](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@IMEngineering](https://forum.inductiveautomation.com/u/IMEngineering)\
**Post date:** [October 22, 2019, 12:27pm UTC](https://forum.inductiveautomation.com/t/db-data-calculation/29272/1 "2019-10-22T12:27:56Z")

</div>

Hello,  
I need to made a calculation of an average value of a flow rate taken by a database interval then I have to calculate the total product consumption in the same interval and I have no idea at all on how to create the script (activated by a button).

Any suggestion?

Thank you  
Luca

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [October 23, 2019, 5:23am UTC](https://forum.inductiveautomation.com/t/db-data-calculation/29272/2 "2019-10-23T05:23:30Z")

</div>

Can you provide some example data to make the explination non-trivial?  
Otherwise, average is the sum of the values divided by the number of values. Consumption is the average flow rate multipled by the time.  
There are a few concepts to use, depending on how accurate you need the result to be. Like using milliseconds in the timestamps.  
Are you wanting a moving average, or just a value between fixed times?

---

<div class="post-metadata">

**Author:** ![IMEngineering](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@IMEngineering](https://forum.inductiveautomation.com/u/IMEngineering)\
**Post date:** [October 23, 2019, 8:20am UTC](https://forum.inductiveautomation.com/t/db-data-calculation/29272/3 "2019-10-23T08:20:03Z")

</div>

Thank you for replying, I’ll try to be more clear:  
I have a flow rate that can be recorded every 1 or 2 seconds. This value is stored into a Database (I have prepared an historical transaction group).  
I know that with “Select AVG(flowRate) From DB” I will have an average value of Flow rate of all recorded values but I have 2 things to do:

1. Select a starting timestamp and an ending timestamp to create an average (I was thinking to use 2 calendar for this purpose) and this should not be a problem. When the interval is selected, I need to count how many records are in this interval to create the total amount of product used and this is something that I don’t know how to do it.
2. I need to put this script in a specific window when is open, the calculation is made and is updated everytime the measure change and not only updated when the windows open or is focused.  
Thank you
