2012-07-17 76 views
-1

我的一位客户遇到OpenERP问题。我需要为他们建立一个自定义对象,因为他们有时会遇到问题,有时他们会在产品销售完成后得到退货产品。所以我需要将其标记为负值并从之前的总售价中扣除。在视图中构建Postgresql视图

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_move需要是否定的,并且总结为具有另一个地方的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,如果我没有把它们放在一起,只看消费者是怎么回事。

+3

那么,你的问题到底是什么? – 2012-07-17 03:04:37

+0

您的视图是否正常工作,您可以使用工作查询创建OpenERP POstgres Views – 2012-07-17 05:42:33

+0

对不起。我太急了,无法重读我写的内容。发生在我们身上。编辑好后,请再看看。 – Allen 2012-07-17 07:05:09

回答

1

万一有人在为此争论。我会在这里粘贴我的答案。如果任何人都可以帮我解释一下为什么在这里工会似乎有效,我就会全神贯注。我可以做简单的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