0

このようなテーブル ( ) があるとします。行の何かPriceHistoryを変更するたびに、行全体をテーブルに再度記録します。

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

BuyPrice行 1、2、3、および 5 を選択する方法はありますか?

これが私が思いついたRubyコードですが、変更された行を選択するだけではありません

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

Ruby と SQL の両方の回答で問題ありません。

4

1 に答える 1

0

これを試して:

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

(@histories.size - 1).times do |n|
  if @histories[n].buy_price != @histories[n+1].buy_price
    puts "changed from : " + @histories[n].id.to_s + " to: " + @histories[n+1].id.to_s
  end
end

幸運を!

于 2012-06-23T04:21:31.220 に答える