我正在尝试更新Postgresql中的一些列但我已经意识到有一些GROUP BY并且有条件和总和功能。 选择语句:
SELECT mp.name , mbl.product_id, "produced",sum(round(mbl.product_qty*smp.product_uom_qty/mb.product_qty,3)) "theo" ,sum(round(smc.product_uom_qty,3)) "real"
FROM stock_move smp LEFT JOIN mrp_production mp ON (mp.id=smp.production_id and smp.state in('done','cancel') )
LEFT JOIN stock_move smc on mp.id=smc.raw_material_production_id
LEFT JOIN mrp_bom mb ON mb.id=mp.bom_id LEFT JOIN mrp_bom_line mbl ON mbl.bom_id=mb.id
WHERE smc.product_id=mbl.product_id and mp.state='done'
GROUP BY mp.name,mbl.product_id having sum(round(mbl.product_qty*smp.product_uom_qty/mb.product_qty,3))!=sum(round(smc.product_uom_qty,3))
我想更新
stock_move.product_uom_qty=sum(mbl.product_qty*smp.product_uom_qty/mb.product_qty)
我试过了:
UPDATE stock_move sm SET product_uom_qty=sum(mbl.product_qty*smp.product_uom_qty/mb.product_qty )
FROM stock_move smp LEFT JOIN mrp_production mp ON (mp.id=smp.production_id and smp.state in('done','cancel') )
LEFT JOIN mrp_bom mb ON mb.id=mp.bom_id LEFT JOIN mrp_bom_line mbl ON mbl.bom_id=mb.id LEFT JOIN stock_move smc ON mp.id=smc.raw_material_production_id
GROUP BY mp.name,mbl.product_id
HAVING SUM(round(mbl.product_qty*smp.product_uom_qty/mb.product_qty,3))!=SUM(round(smc.product_uom_qty,3)))
WHERE sm.product_id=smc.product_id
我得到了:
ERROR: syntax error at or near "group"
LINE 4: group by mp.name,mbl.product_id
答案 0 :(得分:0)
我使用WITH AS
作为以下查询
WITH tabpro AS (
SELECT smc.id,mp.name AS name, mbl.product_id ,sum(smp.product_uom_qty) "produced",sum(round(mbl.product_qty*smp.product_uom_qty/mb.product_qty,3)) AS realqty ,sum(round(smc.product_uom_qty,3)) "REAl" --,mbl.product_uom,smc.product_uom
FROM stock_move smp LEFT JOIN mrp_production mp ON (mp.id=smp.production_id AND smp.state in('done','cancel') ) LEFT JOIN stock_move smc ON mp.id=smc.raw_material_production_id
LEFT JOIN mrp_bom mb ON mb.id=mp.bom_id LEFT JOIN mrp_bom_line mbl ON mbl.bom_id=mb.id
WHERE smc.product_id=mbl.product_id AND mp.state='done' --and mp.name like '%207'
GROUP BY smc.id, mp.name,mbl.product_id
HAVING sum(round(mbl.product_qty*smp.product_uom_qty/mb.product_qty,3))!=sum(round(smc.product_uom_qty,3))
)
UPDATE stock_move sm SET product_uom_qty=tabpro.realqty FROM tabpro
WHERE sm.product_id=tabpro.product_id AND sm.name=tabpro.name AND sm.id=tabpro.id;