SQL Query in Ruby: Only select the changes
- by JJ Liu
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.