0

Situation:

I have this table where I load items into a history, more or less like below:

| date                | item | value |
|---------------------|------|-------|
| 2018-03-08 10:22:00 |   A  | 300   |
| 2018-03-08 10:22:00 |   B  | 3000  |
| 2018-03-08 10:22:00 |   C  | 30    |
| 2018-03-08 10:21:00 |   A  | 200   |
| 2018-03-08 10:21:00 |   B  | 2000  |
| 2018-03-08 10:21:00 |   C  | 20    |
| 2018-03-08 10:20:00 |   A  | 100   |
| 2018-03-08 10:20:00 |   B  | 1000  |
| 2018-03-08 10:20:00 |   C  | 10    |

And I need to calculate the differences between a item entry with the previous item entry, progamatically, resulting in

| date                | item | value | diff |
|---------------------|------|-------|------|
| 2018-03-08 10:22:00 |   A  | 300   | 100  |
| 2018-03-08 10:22:00 |   B  | 3000  | 1000 |
| 2018-03-08 10:22:00 |   C  | 30    | 10   |
| 2018-03-08 10:21:00 |   A  | 200   | 100  |
| 2018-03-08 10:21:00 |   B  | 2000  | 1000 |
| 2018-03-08 10:21:00 |   C  | 20    | 10   |
| 2018-03-08 10:20:00 |   A  | 100   | 100  |
| 2018-03-08 10:20:00 |   B  | 1000  | 1000 |
| 2018-03-08 10:20:00 |   C  | 10    | 10   |

I had saw this link, and that sounded promising, but what I need is one that could build this historical series, because this value will be used into some Grafana dashboards for graphics... Any tips on how I could do this?

I'm using PostgreSQL 9.3 on Linux on System z

HufflepuffBR
  • 443
  • 3
  • 15
  • The linked answer seems to solve your problem. Why isn't it working for you? `lag()` is the best solution to this. –  Mar 08 '18 at 14:09
  • Using the linked solution, it gave me just the last value... What I need is one that calculate all the series, or at least a part of (maybe somewhat 1k lines for each series) – HufflepuffBR Mar 08 '18 at 14:10

0 Answers0