Mysql fifo query stock

孤人 提交于 2019-12-13 09:44:48

问题


I have this mysql table and i need a query to know available pcs of every order distinct by type. Is it possible to do?

Must work as fifo queue
Now i need a query to know how pieces of every order are available

ES

Order Type Available pcs

Planned 16
Cash 10

I buy 5 pcs with order type PLANNED      PLANNED+5
I buy 5 pcs with order type CASH         CASH+5
I buy 5 pcs with order type CASH         CASH+10
I sell 1 pcs                             PLANNED+4   
I sell 1 pcs                             PLANNED+3
1 sell 2 pcs                             PLANNED+1

I buy 15 pcs with order type PLANNED        PLANNED+16   

RESULT    PLANNED+16 CASH+10

回答1:


It this what you are looking for? Your explanation is not very clear, but this would produce the expected results for your sample data:

select 
    coalesce(order_type, 'PLANNED') OrderType,
    sum(case when `Buy/sell` = 'BUY' then qt else -1 * qt end) AvailablePcs
from mytable
group by coalesce(order_type, 'PLANNED')


来源:https://stackoverflow.com/questions/59180181/mysql-fifo-query-stock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!