これは、「タイプ」、「品種」、および「価格」属性を持つテーブルが与えられた場合に、存在する各タイプの最低価格でレコードを取得するという古くからの質問です。
SQL では、次の方法でこれを行うことができます。
select f.type, f.variety, f.price
from ( select type, min(price) as minprice from table group by type ) as x
inner join table as f on f.type = x.type and f.price = x.minprice;`
おそらくこれを次のように模倣できます。
minprices = Table.minimum(:price, :group => type)
result = []
minprices.each_pair do |t, p|
result << Table.find(:first, :conditions => ["type = ? and price = ?", t, p])
end
これよりも優れた実装はありますか?