SQL Query in Ruby: Only select the changes

Posted by JJ Liu on Stack Overflow See other posts from Stack Overflow or by JJ Liu
Published on 2012-06-23T02:55:21Z Indexed on 2012/06/23 3:16 UTC
Read the original article Hit count: 357

Filed under:
|
|
|
|

Suppose I have a table (PriceHistory) like this, every time I change anything in the row, I will record the whole row again in the table.

id | buy_price | sell_price | change_date
 1 |         2 |          2 | 2012-06-22
 2 |         3 |          2 | 2012-06-20
 3 |         2 |          6 | 2012-06-15
 4 |         5 |          5 | 2012-06-15
 5 |         5 |          7 | 2012-06-15
 6 |         4 |          8 | 2012-06-12

I only care about the change of BuyPrice, Is there a way to just select row 1, 2, 3, & 5?

Here is the Ruby code I come up with, but it does not only select the changed rows

PriceHistory.select("id, BuyPrice, change_date").
order("change_date DESC")

Both Ruby and SQL answers are fine.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql