问题
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