0

从 date_start 查询到 date_end,我需要验证在每个日期,给定产品的可用数量是否满足阈值。

@inventories = Inventory.where(['product_id IN (?) AND date_available >= ? AND date_available <= ? AND quantity >= ?', @products, @date_start, @date_end, params[:quantity]]).all

此查询生成在任何给定日期满足条件的库存。但是,对于给定的产品,需要在每个日期满足这些条件。因此,

@products = @inventories.map(&:product).uniq

Q1我现在如何推断所有 available_products (即每个日期的数量 >= ?)和

Q2我需要对每个可用产品的每个日期的价格求和,以便按价格对视图进行排序

*编辑 *

给出建议的答案,我认为提供示例数据以进一步澄清可能是明智的 product_id date_available quantity price_1 price_2
1 ---- 2014-05-27 ---- 3 ---- 35 ---- 60
1 ---- 2014- 05-28 ---- 3 ---- 35 ---- 60
1 ---- 2014-05-29 ---- 3 ---- 25 ---- 50

2 ---- 2014-05-27 ---- 3 ---- 35 ---- 60
2 ---- 2014-05-28 ---- 1 ---- 35 ---- 60
2 ---- 2014-05-29 ---- 3 ---- 35 ---- 60

3 ---- 2014 年 5 月 27 日 ---- 3 ---- 30 ---- 55
3 ---- 2014 年 5 月 28 日 ---- 3 ---- 30 ---- 55
3 ---- 2014-05-29 ---- 3 ---- 30 ---- 55

@price = 'price_1' 和 params[:quantity] 的串联

如果搜索参数是 2014-05-27..2014-05-29,数量 = 2
我应该生成
价格为 165 的
产品 3 价格为 170 的产品 1
产品 2 失败,因为在某个日期未达到阈值。

上下文:rails 3.2.17、postgresql、ruby 1.9.3

4

1 回答 1

0

在这里您可以找到查询,

@inventories = Inventory.where(['product_id IN (?) AND date_available >= ? AND date_available <= ? AND quantity >= ?', @products, @date_start, @date_end, params[:quantity]]).select("date_available as date, product as product, sum(quantity) as quantity, sum(price) as price")).group('date, product').order('price')

从上面的查询中,您可以获得每天的产品数量和价格。

于 2014-05-26T15:10:12.857 回答