# MySQL subtract two rows from column and places into an alias

**URL:** https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316
**Category:** General
**Created:** [19 July 2020 02:08 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316 "2020-07-19T02:08:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sonima](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sonima](https://discourse.nodered.org/u/sonima)
#### Post date: [19 July 2020 02:08 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/1 "2020-07-19T02:08:59Z")

</div>

![database](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/94d41779113b5566a95beeadb172e693a796bf6d.png)

I am working with Node-red and Mysql database. I don't know How to compare previous value with new value on each different devices then insert new value into Data column.  
I mean subtract new value with previous value on each devices then insert that new value into the Data column.  
Anyone help me please ?

---

<div class="post-metadata">

### Author: ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)
#### Post date: [19 July 2020 06:03 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/2 "2020-07-19T06:03:43Z")

</div>

Are you wanting to do it in MySQL or Node Red? It's probably easier in NR by just storing the previous values in the flow context (assuming you have enabled persistent context), and then calculating it before you insert the new row into MySQL.

If you're wanting to do it in MySQL you would have to join the table to itself to get the previous entry. Something along the lines of:

```auto
select current.value - previous.value
from mytable as current
inner join mytable as previous on previous.ID =
    (
        select mytable.ID
        from mytable
        where mytable.Device = current.Device
            and mytable.timestamp < current.timestamp
         order by mytable.timestamp desc
         limit 1
    )

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [19 July 2020 07:53 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/3 "2020-07-19T07:53:13Z")

</div>

A quick search in the node red flows library found this which might be what you want.

> **[node-red-contrib-delta](https://flows.nodered.org/node/node-red-contrib-delta)**
>
> Node-RED node that computes the difference between the current input message and the previous one

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [19 July 2020 08:09 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/4 "2020-07-19T08:09:23Z")

</div>

> [@sonima](#):
>
> I am working with Node-red and Mysql database. I don't know How to compare previous value with new value on each different devices then insert new value into Data column.

If you do mean that you already have data in the table and want to add a difference column then that would generally be considered bad practice as you are storing derived data and you can run into all sorts of problems if the data gets out of phase, for example if data in the table has to be edited then you have to remember to adjust the derived values. Generally it is better to calculate the difference when you need it, possibly using the technique posted by @michaelblight.

---

<div class="post-metadata">

### Author: ![sonima](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sonima](https://discourse.nodered.org/u/sonima)
#### Post date: [19 July 2020 13:07 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/5 "2020-07-19T13:07:51Z")

</div>

Thanks @michaelblight

---

<div class="post-metadata">

### Author: ![sonima](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sonima](https://discourse.nodered.org/u/sonima)
#### Post date: [19 July 2020 13:19 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/6 "2020-07-19T13:19:05Z")

</div>

No, I don't have Data. I mean I need to calculate the Data from current value - previous value then insert in to Data column for each different devices.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 September 2020 13:19 UTC](https://discourse.nodered.org/t/mysql-subtract-two-rows-from-column-and-places-into-an-alias/30316/7 "2020-09-17T13:19:09Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
