sql - Rails 3 Sum Product of two fields -
i need calculate sum of product of 2 fields in rails 3 app (i.e. equivalent excel's sumproduct function). there method in rails , if not, rails code using custom sql?
for example, hotel has many rooms. room has attributes of sqft (square feet), quantity (of size) , hotel_id. calculate total sqft of rooms in given hotel. in sql, hotel.id = 8, believe following statement work:
select sum(rooms.sqft * rooms.quantity) sumsqft rooms inner join hotels on rooms.hotel_id = hotels.id hotels.id = 8;
yep :
room.where(hotel_id: 8).sum("sqft * quantity")
Comments
Post a Comment