【问题标题】:Inventory SQL Query without UNION?没有 UNION 的库存 SQL 查询?
【发布时间】:2011-03-09 09:50:50
【问题描述】:

我正在设计一个库存系统,它有用户、产品、buy_leads、订单(授权购买线索)、输入和输出。

class Product < ActiveRecord::Base
  has_many :buy_leads
end

class BuyLead < ActiveRecord::Base
  belongs_to :product
  has_one :order
end

class Order < ActiveRecord::Base
  belongs_to :buy_lead
  belongs_to :user, :foreign_key => :authorized_by
  has_many :inputs
end

class Input < ActiveRecord::Base
  belongs_to :order
  has_many :outputs
end

class Output < ActiveRecord::Base
  # Associations
  belongs_to :input
  belongs_to :user
end

输入和输出具有数量值。为了获得产品库存和特定产品库存,我在两个原始 SQL 查询中使用 UNION,将输出设为 quantities 负数,然后分组和将它们加在一起:

class InventoryController < ApplicationController

  def index
    @inventory = Input.find_by_sql products_inventory_sql
  end

  def show
    @inventory = Input.find_by_sql product_inventory_sql(params[:id])
  end

private

  def inputs_sql
    "SELECT b.*, p.*, i.order_id,
            i.id AS input_id,
            i.quantity AS quantity     
     FROM inputs i
          JOIN orders r ON r.id = i.order_id
          JOIN buy_leads b ON b.id = r.buy_lead_id
          JOIN products p ON p.id = b.product_id"
  end

  def outputs_sql
    "SELECT b.*, p.*, i.order_id,
            i.id AS input_id,
            (o.quantity * -1) AS quantity
     FROM outputs o
          JOIN inputs i ON i.id = o.input_id
          JOIN orders r ON r.id = i.order_id
          JOIN buy_leads b ON b.id = r.buy_lead_id
          JOIN products p ON p.id = b.product_id"
  end

  def products_inventory_sql
    "SELECT *, SUM(quantity) AS remaining_qty
     FROM (#{inputs_sql} UNION #{outputs_sql})
     GROUP BY product_id"
  end

  def product_inventory_sql(id)
    "SELECT *, SUM(quantity) AS remaining_qty
     FROM (#{inputs_sql} UNION #{outputs_sql})
     WHERE product_id = #{id}
     GROUP BY order_id, input_id"
  end

end

它有效,但我想使用 named_scope 的功能在 ActiveRecord 中链接查询并能够执行以下操作:

Product.inputs.by_product(id)
Product.inventory.by_product(id)
...

有什么想法,还是我必须更改架构以获得更方便的架构?谢谢!

【问题讨论】:

    标签: sql ruby-on-rails sqlite activerecord


    【解决方案1】:

    解决这个问题的可能性太多了,你到底想从这些报告中得到什么?

    买单?产品?输入/输出?

    (我将其发布为答案,因为我无法对您的问题发表评论,如果您能启发我,我会更新答案)

    更新

    试试这个

    #on input
    named_scope :by_product, lambda {|id| {:joins => {:orders => {:buy_leads => :products}}, :conditions => ['products.id = ?', id]}}
    

    您可以通过调用获得与该产品 ID 匹配的输入

    Input.by_product(25)
    

    如果那是您正在寻找的东西,我认为您现在也可以设法按产品制作输出:]

    【讨论】:

    • (输入 - 输出),按产品分类
    【解决方案2】:

    我无法在没有数据的情况下对此进行测试,但我认为应该是这样的:

    SELECT
            b.*, p.*, i.order_id, i.id AS input_id, i.quantity AS quantity, -o.quantity AS quantity,
            (i.quantity - COALESCE(o.quantity,0)) AS remaining_qty
    FROM
            products p
            JOIN buy_leads b ON b.product_id = p.id
            JOIN orders r ON r.buy_lead_id = b.id
            JOIN inputs i ON i.order_id = r.id
            LEFT JOIN outputs o ON o.input_id = i.id
    

    【讨论】:

      【解决方案3】:

      当有多个“输出”记录时,Victor 的解决方案会失败,因为连接将通过输入和输出来复制产品。

      相反,您应该使用派生表而不是实际表进行 JOIN。 如果没有数据,这很难测试和演示,但您应该尝试这样的事情:

          "SELECT b.*, p.*, i.order_id,
              i.id AS input_id,
              i.quantity AS quantity,
      ISNULL(z.negquantities,0) negquantities,
      i.quantity + ISNULL(z.negquantities,0) sumquantities
      
       FROM inputs i
            JOIN orders r ON r.id = i.order_id
            JOIN buy_leads b ON b.id = r.buy_lead_id
            JOIN products p ON p.id = b.product_id
        JOIN 
          (SELECT SUM(-1 * o.quantity) NegQuantities, o.input_id FROM outputs o GROUP BY o.input_id) z
          ON z.input_id = i.id
      

      您看到您正在加入按输入 id 分组的输出表的总和,而不是输出表本身。这消除了连接中意外的行重复。 当然,您可以在派生表的“ON”列表或“Where”子句中添加更多元素(我称之为“z”) 这应该可以让您大部分时间到达那里。 或者,发布一个 DB 图,以便我们更好地了解您的表关系。

      【讨论】:

      • 这两种方法似乎都是正确的,但它们并没有使它更简单,我想使用命名范围
      • 所以把查询放到存储过程中?我想我不确定你在找什么?
      猜你喜欢
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多