Conditional Debit from Credits using SQL
I have one table cryptotransactionledger in below structure where transactions are stored.
TRANSACTION_TYPEID is foreign_key from TRANSACTIONTYPE table (master table).
How can I write a query to implement below rules
- bitcoin-transferred should only be deducted from bitcoin-mined
- bitcoin-lost can be deducted from either bitcoin-received or bitcoin-mined in FIFO manner
Below is the expected result and DB fiddle to above table
transaction_name | Remaining Amount |
---|---|
bitcoin-received | 0 |
bitcoin-mined | 10 |
Logic for remaining balance: bitcoin-received = transaction_typeid 101 - 104 (i.e. 5 - 10. Since bitcoin-received amount is just 5, only 5 coins out of 10 bitcoin-lost will be deduced from here. Remaining 5 will be deducted from bitcoin-mined in FIFO manner)
bitcoin-mined = transaction_typeid 102-103-104 (i.e. 20 - 5 - 5(balance after deduction from bitcoin-received (above line)))
http://sqlfiddle.com/#!4/0fd02/1
Thanks in advance
from Recent Questions - Stack Overflow https://ift.tt/2XW8u2O
https://ift.tt/3ieYKbp
Comments
Post a Comment