我正在尝试创建一个使用Java作为前景并且以Mysql作为数据库的外汇交易程序,目前存在通过汇总(购买金额*汇率)-(销售金额*汇率)来计算利润/亏损的问题。我有2个表(Buy&Sell)会更新,这取决于我是否在TransactionTable上输入了交易,并且在SellTable上有定界符,每当我在SellTable上“插入数据”时都会更新股票的BuyTable,
例如,我的BuyTable上有一个Buy输入,该输入已从Java程序插入。
> select * from TableBuy;
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
| TransactionCode | TraderId | TransactionTime | Currency | Balance Bought | Rate | Sold Balance | Remaining Balance | SellRate | Processed |
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
| 1 | 32547AND | 2019-05-07 15:41:14 | USD | 100 | 9650 | 100 | 0 | 9700 | Y |
| 2 | 32547AND | 2019-05-07 15:41:41 | USD | 50 | 9600 | 25 | 25 | 9700 | Y |
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
2 rows
哪些股票已通过我的SellTable中创建的触发器进行了更新, 这是定界符和SellTable,触发器基本上会更新BuyTable上面显示的剩余余额的Buy Table和Sold余额
> select * from SellTable;
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
| TransactionCode | TraderId | TransactionDate | Currency | Balance Sold | SellRate |
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
| 1 | 32547AND | 2019-05-07 15:47:00 | USD | 125 | 9700 |
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
1 rows
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `Test`.`fifosell`$$
create trigger `Test`.`fifosell` BEFORE INSERT on `Test`.`SellTable`
for EACH ROW BEGIN
DECLARE AMOUNT INT;
SET AMOUNT=NEW.'Balance Sold';
SET @RATE =New.SellRate;
SET @ID=(SELECT TransactionCode FROM BuyTable WHERE Currency=NEW.Currency AND TraderId=NEW.TraderId AND TransactionDate=(SELECT TransactionDate FROM BuyTable WHERE Currency=NEW.Currency AND TraderId=NEW.TraderId AND 'Remaining Balance'>0 LIMIT 1) AND Remaining Balance>0 LIMIT 1);
SET @REMAIN=(SELECT 'Remaining Balance' FROM TableBuy WHERE TransactionCode =@ID);
SET @Currency=NEW.Currency;
REPEAT
IF(@REMAIN>AMOUNT) THEN
UPDATE BuyTable SET 'Remaining Balance'=@REMAIN-AMOUNT, 'Sold Balance'='Sold Balance'+AMOUNT, SellRate = @Rate , Processed = 'N' WHERE TransactionCode=@ID AND Currency=@Currency;
SET AMOUNT=0;
END IF;
IF (@REMAIN<AMOUNT) THEN
UPDATE TableBuy SET 'Remaining Balance'=0,'Sold Balance'='Sold Balance'+AMOUNT , SellRate = @Rate , Processed = 'N' WHERE TransactionCode=@ID AND Currency=@Currency;
SET AMOUNT=AMOUNT-@REMAIN;
SET @ID=(SELECT TransactionCode FROM BuyTable WHERE Currency=NEW.Currency AND TraderId=NEW.TraderId AND TransactionDate=(SELECT TransactionDate FROM BuyTable WHERE Currency=NEW.Currency AND TraderId=NEW.TraderId AND 'Remaining Balance'>0 LIMIT 1) AND Remaining Balance>0 LIMIT 1);
SET @REMAIN=(SELECT 'Remaining Balance' FROM TableBuy WHERE TransactionCode =@ID);
IF(@REMAIN=AMOUNT) THEN
UPDATE TableBuy SET 'Remaining Balance'=0,'Sold Balance'='Sold Balance'+AMOUNT, SellRate = @Rate , Processed = 'N' WHERE TransactionCode=@ID AND Currency=@Currency;
SET AMOUNT=0;
END IF;
UNTIL AMOUNT=0
END REPEAT;
END;
$$
DELIMITER ;
在对SellTable的第二次输入销售时,触发器将BuyTable的第二行的余额和SellRate更新,该行的25余额为0余额,然后使用下面的更新查询更新TransactionsTable上的ProfitLoss列
> select * from SellTable;
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
| TransactionCode | TraderId | TransactionDate | Currency | Balance Sold | SellRate |
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
| 1 | 32547AND | 2019-05-07 15:47:00 | USD | 125 | 9700 |
| 2 | 32547AND | 2019-05-07 16:49:15 | USD | 25 | 9900 |
+ -------------------- + ------------- + -------------------- + ------------- + ----------------- + ------------- +
2 rows
> select * from BuyTable;
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
| TransactionCode | TraderId | TransactionTime | Currency | Balance Bought | Buy Rate | Sold Balance | Remaining Balance | SellRate | Processed |
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
| 1 | 32547AND | 2019-05-07 15:41:14 | USD | 100 | 9650 | 100 | 0 | 9700 | Y |
| 2 | 32547AND | 2019-05-07 15:41:41 | USD | 50 | 9600 | 50 | 0 | 9900 | Y |
+ -------------------- + ------------- + -------------------- + ------------- + ------------------- + --------- + ----------------- + ---------------------- + ------------- + -------------- +
2 rows
这是我对更新“交易”标签上的ProfitLoss列的查询
UPDATE TransactionsTable SET ProfitLoss =(select sum(SellRate* 'Sold Balance') - sum(Buy Rate * 'Sold Balance') From BuyTable Where SellRate='9700' AND TraderId ='32547AND' AND Processed = 'Y') ORDER BY TransactionCode DESC LIMIT 1;
当我在SellTable上输入另一个Selldata时,如在下面的交易表中所示,获取新的损益率时遇到问题
> select * from TransactionsTable;
+ -------------------- + ------------- + -------------------- + ----------------- + ------------ + --------- + ----------- + ----------- + ----------- +
| TransactionCode | TraderID | TransactionDate | CurrencyCode | BuySell | Rate | Amount | Profit | Remark |
+ -------------------- + ------------- + -------------------- + ----------------- + ------------ + --------- + ----------- + ----------- + ----------- +
| 1 | 32547AND | 2019-05-07 15:41:14 | USD | B | 9650 | 100 | | |
| 2 | 32547AND | 2019-05-07 15:41:41 | USD | B | 9600 | 50 | | |
| 3 | 32547AND | 2019-05-07 15:47:00 | USD | S | 9700 | 125 | 7500 | |
| 4 | 32547AND | 2019-05-07 16:49:15 | USD | S | 9900 | 25 | 15000 | |
+ -------------------- + ------------- + -------------------- + ----------------- + ------------ + --------- + ----------- + ----------- + ----------- +
4 rows
根据FIFO方法,TransactionsTable的最后一行应具有25 *(9900-9600),其结果应为7500,其中25 * 9600是第二行的剩余购买余额,而25 * 9900是从第四行
是否存在可以在触发器或更新查询中进行修改/添加以获取实际计算的查询?