# Difference between two hours min values

**URL:** <https://community.grafana.com/t/difference-between-two-hours-min-values/40809>\
**Category:** InfluxDB\
**Created:** [December 19, 2020, 11:33pm UTC](https://community.grafana.com/t/difference-between-two-hours-min-values/40809 "2020-12-19T23:33:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kubrik](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kubrik](https://community.grafana.com/u/kubrik)\
**Post date:** [December 19, 2020, 11:33pm UTC](https://community.grafana.com/t/difference-between-two-hours-min-values/40809/1 "2020-12-19T23:33:56Z")

</div>

Hi!  
I’ve been playing with grafana for a few days and I am facing a dilemma 😃  
How could I make the difference between the minimum of the next hour and the minimum of the previous one?

Case:  
I’ve a sensor counter (Energy), and till now i’m calculating kwh with this query:

`SELECT (last(value)-first(value)) FROM Usage WHERE $timeFilter GROUP BY time(1h)`

Generally it seems to be fine but sometimes there are cases like these:

 ![hour_-_Grafana](https://us1.discourse-cdn.com/grafana/original/2X/2/24ea5d8a2e9f2e7feaa9597a2edda78df3488191.png)

As you can see, there is a difference, lost, between 19:41:38 (last value) and 20:00:34 (first value next hour).  
So, I suppose, the best calculation should be between the first value of the hour and the first value of the next hour.

Is it possible? How?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![jangaraj](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jangaraj/32/39197_2.png) [@jangaraj](https://community.grafana.com/u/jangaraj)\
**Post date:** [December 20, 2020, 4:07am UTC](https://community.grafana.com/t/difference-between-two-hours-min-values/40809/2 "2020-12-20T04:07:33Z")

</div>

Use the InfluxDB subqueries. Inner query will calculate hourly mins and outer query will calculate their diffs.

But I would use more generic approach: calculate derivation (because you have counter) and then do a hourly sums (again task for subquery). Derivation of counter values is frequently asked question (especially for power measurements) also on this forum - just use search functionality for more discussions about this topic.

---

<div class="post-metadata">

**Author:** ![kubrik](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kubrik](https://community.grafana.com/u/kubrik)\
**Post date:** [December 20, 2020, 6:06pm UTC](https://community.grafana.com/t/difference-between-two-hours-min-values/40809/3 "2020-12-20T18:06:34Z")

</div>

Hi @jangaraj,  
thank you for your reply.  
I’ve searched around in the forum but i’ve not found any example that’s right for me.  
Could you provide me an example of query/subquery for my purpose?  
I would appreciate it very much.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kubrik](https://avatars.discourse-cdn.com/v4/letter/k/a183cd/32.png) [@kubrik](https://community.grafana.com/u/kubrik)\
**Post date:** [December 20, 2020, 9:33pm UTC](https://community.grafana.com/t/difference-between-two-hours-min-values/40809/4 "2020-12-20T21:33:04Z")

</div>

I think i’ve found the right way…

```
select difference(last) from (
select last(value) from Usage group by time(1h) tz('Europe/Rome'))

```

gives me reasonable results.

Thank you again for the tip 🙂
