【发布时间】:2010-09-16 04:20:10
【问题描述】:
这是一个古老的问题,给定一个具有“类型”、“品种”和“价格”属性的表格,您可以获取每种类型的最低价格记录。
在 SQL 中,我们可以通过:this:
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
还有比这更好的实现吗?
【问题讨论】:
-
如何获得每种类型的最高和最低价格??
标签: activerecord group-by aggregate-functions minimum