计算股票 OpeningQuantity、QuantityIn、QuantityOut 和 ClosingQuantity

Calculate stock OpeningQuantity, QuantityIn, QuantityOut and ClosingQuantity

我有两个 tables Item 和 LineItem。

项Table

ItemId Name Quantity
1 Shampoo 2
2 Soap 0

订单项Table

LineItemId ItemId QuantityIn QuantityOut DateCreated
1 1 1 0 2021-05-04
2 1 1 0 2021-05-05

现在,如果我想 return 在 2021-05-01 和 2021-05-04 之间使用查询 return OpeningQuantity、QuantityIn、QuantityOut 和 CurrentQuantity,它应该 return 像这样:

ItemId Name Begining Quantity QuantityIn QuantityOut CurrentQuantity
1 Shampoo 0 1 1

如果我想 return 在 2021-05-04 和 2021-05-05 之间使用查询 return OpeningQuantity、QuantityIn、QuantityOut 和 CurrentQuantity,它应该 return 像这样:

ItemId Name Begining Quantity QuantityIn QuantityOut CurrentQuantity
1 Shampoo 1 1 2
select
    items.ItemId as id,
    items.name as name,
    (
        select sum(quantityin - quantityout)
        from lineitems
        where
            ItemId = items.ItemId
            and datecreated < '2021-05-04'
    ) as beginning,
    (
        select sum(quantityin)
        from lineitems
        where
            ItemId = items.ItemId
            and datecreated >= '2021-05-04'
            and datecreated <= '2021-05-04'
    ) as quantity_in,
    (
        select sum(quantityout)
        from lineitems
        where
            ItemId = items.ItemId
            and datecreated >= '2021-05-04'
            and datecreated <= '2021-05-04'
    ) as quantity_out,
    (
        (
            select sum(quantityin - quantityout)
            from lineitems
            where
                ItemId = items.ItemId
                and datecreated < '2021-05-04'
        ) + sum(lineitems.QuantityIn - lineitems.QuantityOut)
    ) as closing_quantity
from
    items
    inner join LineItems as lineitems on items.itemid = LineItems.itemid
where
    lineitems.DateCreated >= '2021-05-04'
    and lineitems.DateCreated <= '2021-05-04'
group by
    items.name,
    items.ItemId

我试过这个查询,它使用子查询提取每一列数据,但是 closing_quantity 没有 returning 任何东西,而且这个查询似乎效率不高,因为对于 beginning_quantity 我们有从上面的日期到第一个记录搜索整个 table 并求和。

任何人都可以帮助我应该使用什么查询或重新设计 tables。

如果我没理解错的话,这就是你想要做的:

select   
    items.ItemId
    , items.name
    ,coalesce(sum(quantityin-quantityout) filter (where DateCreated <= '2021-05-01'),0)  beginning
    ,sum(quantityin) filter (where DateCreated > '2021-05-01') QuantityIn
    ,sum(quantityout) filter (where DateCreated > '2021-05-01') QuantityOut
    ,sum(quantityin - quantityout)  CurrentQuantity
from items
inner join LineItems 
    on items.itemid = LineItems.itemid
where lineitems.DateCreated <= '2021-05-04'
group by
    items.name,items.ItemId;

我用 postgresql 语法写的,这里是标准的 sql 语法:

select   
    items.ItemId
    , items.name
    ,sum(case when DateCreated <='2021-05-01' then quantityin-quantityout else 0 end) beginning
    ,sum(case when DateCreated > '2021-05-01' then quantityin else 0 end) QuantityIn
    ,sum(case when DateCreated > '2021-05-01' then quantityout else 0 end) QuantityOut
    ,sum(quantityin - quantityout)  CurrentQuantity
from items
inner join LineItems 
    on items.itemid = LineItems.itemid
where lineitems.DateCreated <= '2021-05-04'
group by
    items.name,items.ItemId;