我的一位 OpenERP 客户遇到了问题。我需要为他们构建一个自定义对象,因为他们有一个问题,有时他们在售出后会收到退货。所以我需要将其标记为负数,并从之前的销售总额中减去。
SELECT sum(rsm1.product_qty_out) as "out"
, sum(rsm2.product_qty_in) as "in"
, rsm2.location_id
, rsm2.product_id
, rsm2.month
from report_stock_move as rsm1, report_stock_move as rsm2
where rsm2.location_id in (
select id
from stock_location
where "name" = 'Consumers'
)
and rsm1.location_id not in (
select id
from stock_location
where "name" = 'Consumers'
)
and rsm1.location_dest_id = rsm2.location_id
and rsm2.location_dest_id = rsm1.location_id
and rsm1.state = 'done' -- both are in done state
and rsm2.state = rsm1.state
group by rsm2.location_id, rsm2.month, rsm2.product_id
;
有人有想法么?此外,根据我用于分组的表,我不断得到不同的结果。这是为什么?
编辑 对不起,我急于解决这个问题,我似乎已经明确了我的观点。来自 stock_location 表的具有消费者 location_id 的 report_stock_moves 需要为负,并汇总到具有另一个地方的 location_id 的 report_stock_moves 中,但 Consumer 是 location_dest_id。
例子:
location_id = Some Place (actually ID of stock_location of course)
product_qty = 8
location_dest_id Consumers
date = 2012-07-02
location_id = Consumer
product_qty = 4
location_dest_id Some Place
date = 2012-07-04
这里有两个记录示例。他们附有不同的日期,所以我可能想取最大日期,或者如果我想将它们合并或分组,我可能想取最大日期或退货日期(从消费者到某个地方),以便当它们放在一起时,我得到 4,而不是说 8,如果我没有把它们放在一起,只看消费者的情况。