SQL每月计算股票

时间:2015-12-24 10:51:02

标签: postgresql openerp openerp-8 stocks

我有特定的任务,不知道如何实现它。我希望有人可以帮助我=)

我有stock_move表:

product_id |location_id |location_dest_id |product_qty |date_expected       |
-----------|------------|-----------------|------------|--------------------|
327        |80          |84               |10          |2014-05-28 00:00:00 |
327        |80          |84               |10          |2014-05-23 00:00:00 |
327        |80          |84               |10          |2014-02-26 00:00:00 |
327        |80          |85               |10          |2014-02-21 00:00:00 |
327        |80          |84               |10          |2014-02-12 00:00:00 |
327        |84          |85               |20          |2014-02-06 00:00:00 |
322        |84          |80               |120         |2015-12-16 00:00:00 |
322        |80          |84               |30          |2015-12-10 00:00:00 |
322        |80          |84               |30          |2015-12-04 00:00:00 |
322        |80          |84               |15          |2015-11-26 00:00:00 |

即。它的产品表从一个仓库转移到第二个仓库。

如果我使用类似的东西,我可以在自定义日期计算库存:

select
    coalesce(si.product_id, so.product_id) as "Product",
    (coalesce(si.stock, 0) - coalesce(so.stock, 0)) as "Stock"
from
    (
        select
            product_id
            ,sum(product_qty * price_unit) as stock
        from stock_move
        where
            location_dest_id = 80
            and date_expected < now()
        group by product_id
    ) as si
    full outer join (
        select
            product_id
            ,sum(product_qty * price_unit) as stock
        from stock_move
        where
            location_id = 80
            and date_expected < now()
        group by product_id
    ) as so
    on si.product_id = so.product_id

结果我有现货:

Product |Stock |
--------|------|
325     |1058  |
313     |34862 |
304     |2364  |

但如果我每月需要库存该怎么办? 这样的事情?

Month   |Total Stock |
--------|------------|
Jan     |130238      |
Feb     |348262      |
Mar     |2323364     |

如何将产品数量从每个月的开始时间到每个月结束?

我只有一个想法 - 每个月使用24个子查询获取股票(例如下面)

Jan |Feb | Mar |
----|----|-----|
123 |234 |345  |

旋转行和列后结束? 我觉得这很愚蠢,但我不知道另一种方式......帮助我pls =)

1 个答案:

答案 0 :(得分:0)

这样的事情可以为您提供每月“结束”的库存快照。诀窍是你的数据可能会省略某些部分的某些月份,但那部分仍然会有余额(即1月收到50,2月没有发生任何事情,但你仍然想要显示2月份的总计50个)。

处理此问题的一种方法是提出所有可能的组合部分/日期。我在这个例子中假设了1/1/14 + 24个月,但在all_months子查询中很容易改变。例如,您可能只想从stock_move表中的最小日期开始。

with all_months as (
  select '2014-01-01'::date + interval '1 month' * generate_series(0, 23) as month_begin
),
stock_calc as (
  select
    product_id, date_expected,
    date_trunc ('month', date_expected)::date as month_expected,
    case
      when location_id = 80 then -product_qty * price_unit
      when location_dest_id = 80 then product_qty * price_unit
      else 0
    end as qty
  from stock_move
  union all
  select distinct
    s.product_id, m.month_begin::date, m.month_begin::date, 0
  from
    stock_move s
    cross join all_months m
),
running_totals as (
  select
    product_id, date_expected, month_expected,
    sum (qty) over (partition by product_id order by date_expected) as end_qty,
    row_number() over (partition by product_id, month_expected
      order by date_expected desc) as rn
  from stock_calc
)
select
  product_id, month_expected, end_qty
from running_totals
where
  rn = 1
相关问题