【问题标题】:join two query postgresql加入两个查询 postgresql
【发布时间】:2017-02-22 21:24:39
【问题描述】:

我正在尝试混合两个查询 postgresql

select stock_quant.product_id as prod_id, product_product.name_template as name ,sum(stock_quant.qty) as qty_quant
from stock_quant
join product_product on stock_quant.product_id = product_product.id
join stock_location on stock_quant.location_id = stock_location.id
where stock_location.usage in ('internal','transit')
group by stock_quant.product_id, product_product.name_template
order by stock_quant.product_id

这个给我的产品总数量在表 stock_quant 中 ,第二个查询让我得到另一个表“stock_history”中的数量总和

select stock_history.product_id as prod_id,product_product.name_template as name , sum(stock_history.quantity) as qty_history
from stock_history
join product_product on stock_history.product_id = product_product.id
group by stock_history.product_id,name
order by stock_history.product_id

好吧,我想做的是只显示两个总和对于相同产品 ofc 相等的产品 thnx 我真的很感谢一些解释

【问题讨论】:

  • 好吧,如果您能提供表的架构,那就太好了。

标签: postgresql odoo-8


【解决方案1】:

答案是:

select stock_quant.product_id as prod_id, product_product.name_template as name ,sum(stock_quant.qty) as qty_quant
from stock_quant
join product_product on stock_quant.product_id = product_product.id
join stock_location on stock_quant.location_id = stock_location.id
where stock_location.usage in ('internal','transit') 
group by stock_quant.product_id, product_product.name_template
having sum(stock_quant.qty) != (
    select sum(stock_history.quantity) as qty_history
    from stock_history
    where stock_quant.product_id = stock_history.product_id )
order by stock_quant.product_id

目前我在工作,如果需要我会在稍后为你们提供更多解释 谢谢

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2010-10-08
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多