0

Hello all,

using mysql

I have the folowing (example) table

+------------+--------+
| date | total |
+------------+--------+
| 2009-01-08 | 150000 |
| 2009-01-09 | 151000 |
| 2009-01-10 | 156000 |
| 2009-01-11 | 154000 |
| 2009-01-12 | 153000 |
| 2009-01-13 | 157000 |
+------------+--------+

Now i want to make a query that counts the diffrence with the day befor.
I has looked ad some running total query's, but cant find a way to creat a correct query.

The results should look like this:

+------------+--------+-------+
| date | total | diff |
+------------+--------+-------+
| 2009-01-08 | 150000 | |
| 2009-01-09 | 151000 | 1000 | -> 151000 - 150000
| 2009-01-10 | 156000 | 5000 | -> 156000 - 151000
| 2009-01-11 | 154000 | -2000 | -> 154000 - 156000
| 2009-01-12 | 153000 | -1000 | -> 153000 - 154000
| 2009-01-13 | 157000 | 4000 | -> 157000 - 153000
+------------+--------+-------+

( the -> ...... are the counting that must be done and isnt part of the result)

I will be very happy if someone can help me with this.

Thx,

Nobloz

1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by nobloz
0

Found the solution for this. thx anyway.

SELECT t1.date 
,      t1.total 
,      t1.total 
   - ( SELECT t2.total 
       FROM   tabel t2 
       WHERE  t2.date = DATE_SUB( t1.date, INTERVAL 1 DAY ) 
     ) diff 
FROM   tabel t1;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.