MySQL Forums :: Newbie :: compare two rows in the same column


Advanced Search

compare two rows in the same column
Posted by: Mark Lane ()
Date: September 15, 2009 05:42PM

I have the following table (for example);

+---------+------------+
| Date | Value |
+---------+------------+
| 1/1/09 | 15 |
| 1/2/09 | 11 |
| 1/3/09 | 6 |
| 1/4/09 | 14 |
| 1/5/09 | 20 |
| 1/6/09 | 3 |
+---------+------------+

I need to write a query to return the change in value for each day. In other words I need to subtract each day's value from the previous day's value. Here is an example of the query's results...

+---------+------------+
| Date | Change |
+---------+------------+
| 1/2/09 | 4 |
| 1/3/09 | 5 |
| 1/4/09 | -8 |
| 1/5/09 | -6 |
| 1/6/09 | 17 |
+---------+------------+

Note the query does not retun a row for 1/1/09 because there is no previous value to compare it to. I'm really struggling with this one... any tips???

Options: ReplyQuote


Subject Written By Posted
compare two rows in the same column Mark Lane 09/15/2009 05:42PM
Re: compare two rows in the same column laptop alias 09/15/2009 05:50PM
Re: compare two rows in the same column Gavin Taylor 10/16/2009 11:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.