-1

我的一位 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,如果我没有把它们放在一起,只看消费者的情况。

4

1 回答 1

1

以防以后有人为此而苦苦挣扎。我会在这里粘贴我的答案。如果有人可以帮助我解释为什么 union all in here 似乎有效,我会全力以赴。我可以做简单的 SQL,但我不太了解联合是如何工作的,尤其是在这种情况下。

select max(total.id) as id,
                     sum(total.product_qty) as product_qty,
                     total.location_id as location_id,
                     total.date as "date",
                     total.year as year,
                     total.month as month,
                     total.day as day,
                     total.product_id as product_id
                     from (
                       -- From Consumer back to location
                        select -max(id) as id,
                        -sum(product_qty) as product_qty, 
                        location_dest_id as location_id, 
                        "date", year, month, day, product_id
                        from report_stock_move
                        where location_id in (
                            select id
                            from stock_location
                            where "name" = 'Consumers'
                        )
                        and "state" = 'done'
                        group by location_dest_id, "date", year, month, day, product_id

                        union all

                        -- From Location to Consumer
                        select max(id) as id,
                        sum(product_qty) as product_qty, 
                        location_id, "date", year, month, day, product_id
                        from report_stock_move
                        where location_id not in (
                            select id
                            from stock_location
                            where "name" = 'Consumers'
                        )
                        and "state" = 'done'
                        and location_dest_id in (
                            select id
                            from stock_location
                            where "name" = 'Consumers'
                        )
                        group by location_id, "date", year, month, day, product_id
                     ) as total
                     group by location_id, "date", year, month, day, product_id
于 2012-07-19T02:19:02.017 回答