samedi 2 juillet 2016

Fifo sorting a sql table


I have a table called Transactions which looks like this: Id | date | type | price | quantity | buysell 1 | 0001 | 1 | 1.00 | 3 | buy 2 | 0002 | 2 | 5.00 | 1 | buy 3 | 0003 | 3 | 0.30 | 2 | buy 4 | 0006 | 1 | 1.50 | 1 | sell 5 | 0007 | 4 | 7.00 | 12 | buy 6 | 0011 | 2 | 6.00 | 1 | sell 7 | 0015 | 3 | 0.50 | 2 | sell 8 | 0016 | 1 | 1.50 | 2 | sell 9 | 0017 | .... I need to find a way how to match all sell transactions to related buy transactions. This should be done in the fifo principle: In the example above, transaction 4 should match to 1, 6 to 2, 7 to 3 and 8 to 1 (depending on the type). Also the quantity field has to be considered - transaction 1 for example has two fulfilling sell transactions which sum up to the buy quantity of 3. Is there a way to achieve this with mysql or any other database system? Edit The desired resulset could for example be a table of all sell transactions and their related buy transactions: Id | sellid | buyid 1 | 4 | 1 2 | 6 | 2 3 | 7 | 3 4 | 8 | 1 Based on this table, I can calculate margins or the time it takes to sell something (for example).

Aucun commentaire:

Enregistrer un commentaire