加入两个查询postgresql

时间:2017-02-22 21:24:40

标签: postgresql odoo-8

即时尝试混合两个查询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
嗯,我想要做的只是展示两种产品的相同产品的相同产品 thnx我真的很感激一些解释

1 个答案:

答案 0 :(得分:0)

答案是:

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

目前我正在工作,如果需要,我会在稍后向你们提供更多解释 日Thnx