# Create col of two rows subtracted in query

**URL:** <https://forum.inductiveautomation.com/t/create-col-of-two-rows-subtracted-in-query/75314>\
**Category:** Ignition\
**Tags:** ignition78, vision\
**Created:** [June 21, 2023, 4:44am UTC](https://forum.inductiveautomation.com/t/create-col-of-two-rows-subtracted-in-query/75314 "2023-06-21T04:44:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [June 21, 2023, 4:44am UTC](https://forum.inductiveautomation.com/t/create-col-of-two-rows-subtracted-in-query/75314/1 "2023-06-21T04:44:00Z")

</div>

Hello,

I have a table with cols grand total and GPM

I'd like to add a col in-between. Col B = Col A Row B - Col A Row A and keep that pattern sequentially down the column for the table.

I get the basic SQL query, but am unsure of how to keep that pattern sequentially turning out to the table.

Any ideas or tips in the right direction would be much appreciated.

---

<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:** [June 21, 2023, 10:18am UTC](https://forum.inductiveautomation.com/t/create-col-of-two-rows-subtracted-in-query/75314/2 "2023-06-21T10:18:30Z")

</div>

Given the following table called `totalizer` (I'm really hip with names):  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/5/554f3f9fc9907c3d3bb3bd77eada4028c9d279cc.png)

Use of a LAG function should do it.

```sql
SELECT total,
       total - LAG(total) OVER (ORDER BY ndx) AS difference,
       gpm
FROM totalizer

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/4/c4041e03b98c6020cab2bd916ad0be57ae28cb9a.png)

---

<div class="post-metadata">

**Author:** ![andrews](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@andrews](https://forum.inductiveautomation.com/u/andrews)\
**Post date:** [June 21, 2023, 2:13pm UTC](https://forum.inductiveautomation.com/t/create-col-of-two-rows-subtracted-in-query/75314/3 "2023-06-21T14:13:50Z")

</div>

That’s perfect! Thank you !
