# SQL query to calculate Difference

**URL:** <https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807>\
**Category:** Ignition\
**Tags:** ignition81, namedqueries, scripting\
**Created:** [July 12, 2022, 5:00pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807 "2022-07-12T17:00:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pratik\_jain](https://avatars.discourse-cdn.com/v4/letter/p/97f17d/32.png) [@pratik\_jain](https://forum.inductiveautomation.com/u/pratik_jain)\
**Post date:** [July 12, 2022, 5:00pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/1 "2022-07-12T17:00:13Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/9/1970f440d2e7472061b36367bbb51ba7b4b9e349.png)  
Hello all,  
I have a transaction group running on a trigger from PLC. This transaction group records the values and stores them into the DB. The next step i am trying to calculate the difference between two rows of AWN Totalizer and RODI totalizer and store them in a new column as AWN FLOW and RODI FLOW. I have heard about the LAG function but not sure how to go about storing and triggering that query.

Thank You.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 12, 2022, 5:05pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/2 "2022-07-12T17:05:20Z")

</div>

You would probably need to write a named query, which you then called from a Gateway Event (perhaps a Tag Change Event).

I don’t think it is possible to do this calculation from a transaction group.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 12, 2022, 7:12pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/3 "2022-07-12T19:12:58Z")

</div>

On your DB query to get that table you can add the columns for viewing purposes easily, but they wouldn’t be written to the DB itself. If that’s all you need ?

---

<div class="post-metadata">

**Author:** ![pratik\_jain](https://avatars.discourse-cdn.com/v4/letter/p/97f17d/32.png) [@pratik\_jain](https://forum.inductiveautomation.com/u/pratik_jain)\
**Post date:** [July 13, 2022, 2:50pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/4 "2022-07-13T14:50:30Z")

</div>

Yes that would work too. can you please give me more information on how to calculate the difference ?

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 13, 2022, 3:12pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/5 "2022-07-13T15:12:14Z")

</div>

What are you specifically trying to work out and I’ll see if I can work out the query for you. Will be a good learning experience for me too.

---

<div class="post-metadata">

**Author:** ![pratik\_jain](https://avatars.discourse-cdn.com/v4/letter/p/97f17d/32.png) [@pratik\_jain](https://forum.inductiveautomation.com/u/pratik_jain)\
**Post date:** [July 13, 2022, 3:56pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/6 "2022-07-13T15:56:46Z")

</div>

I am trying to calculate the difference between the AWN Totalizer from one day and previous day and store it in a new column in the same table as AWN Flow.

eg, AWN Totalizer on Day2 - AWN totalizer on day 1 store it in as AWN Flow in same row.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 13, 2022, 4:10pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/7 "2022-07-13T16:10:42Z")

</div>

I do something similar for production values, difference between today and yesterday, let me see if I can help and apply that to your example.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [July 13, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/8 "2022-07-13T16:13:34Z")

</div>

You could also do this in scripting, something that may be helpful for you,

> [@Tag history difference over interval](https://forum.inductiveautomation.com/t/tag-history-difference-over-interval/48366):
>
> I am trying to find a better way to do production counts over specific intervals using tag history. Example, I have a tag TestCount, right now it’s a memory tag, but typically this would be an OPC tag from a PLC that increments. I can’t use Count aggregationMode because it may not “catch” each transition, example, it may go from 10 to 13. My desire is to be able to chart this data over a period of time, example, 12 hours, with a certain interval, example 1 hour. So, if at the start of the ho…

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 14, 2022, 7:17pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/9 "2022-07-14T19:17:49Z")

</div>

So I took a different approach to this…

I created a Query Tag Called Last Entry.

```auto
Select AWN
From testdb
WHERE id=(SELECT max(id) FROM testdb)

```

This gets the AWN value from the last entry in the table, assuming id is your incrementing key.

I then created a expression tag…

```auto
{[.]FlowMeter}-{[.]Last Entry}

```

This is assuming your live flow meter value is a tag. So this is taking your real time flow value, subtracting the result from the last DB entry.

The result of this query tag can be put in your transaction group.  
You can also use this value then to show a live delta, if anyone wants to see that.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/d/0d6765f691f1b4d3d5aceebefc98224c22046776.png)

---

<div class="post-metadata">

**Author:** ![pratik\_jain](https://avatars.discourse-cdn.com/v4/letter/p/97f17d/32.png) [@pratik\_jain](https://forum.inductiveautomation.com/u/pratik_jain)\
**Post date:** [July 18, 2022, 2:33pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/10 "2022-07-18T14:33:16Z")

</div>

Thank You ! I have created the tags and added them to the transaction group. Will let you in couple of days if this works out.

Appreciate your help !

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 21, 2022, 6:32pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/11 "2022-07-21T18:32:40Z")

</div>

How did this workout for you ?

---

<div class="post-metadata">

**Author:** ![pratik\_jain](https://avatars.discourse-cdn.com/v4/letter/p/97f17d/32.png) [@pratik\_jain](https://forum.inductiveautomation.com/u/pratik_jain)\
**Post date:** [August 2, 2022, 4:21pm UTC](https://forum.inductiveautomation.com/t/sql-query-to-calculate-difference/61807/12 "2022-08-02T16:21:39Z")

</div>

It worked out perfectly ! I was able to get the values. Thank you so much !
