我有一个名为ProfitCalculation的程序。这需要三个参数......程序正文如下:
delimiter //
CREATE PROCEDURE ProfitCalculation
(
IN instrument INT(20) ,
OUT buy float(10,3) ,
OUT sell float(10,3) ,
OUT profit float(10,3)
)
BEGIN
SELECT SUM(FldLastTradeQuantity*FldPrice) as total_buy
INTO buy
FROM TblOrders
WHERE FldInstrumentID = instrument AND FldBuySell = 'b' AND FldLastTradePrice != 0 AND FldLastTradeQuantity != 0 group by FldInstrumentID;
SELECT SUM(FldLastTradeQuantity*FldPrice) as total_buy
INTO sell
FROM TblOrders
WHERE FldInstrumentID = instrument AND FldBuySell = 's' AND FldLastTradePrice != 0 AND FldLastTradeQuantity != 0 group by FldInstrumentID;
profit = sell - buy;
END
delimiter ;
现在我想计算利润。但它只返回1。 请给我一个解决方案,它将返回实际结果。
答案 0 :(得分:1)
此过程可以简化为单个SQL查询,这可以帮助您进行调查。
SELECT buy, sell, (sell - buy) profit FROM (
SELECT
SUM(CASE FldBuySell WHEN 'b' THEN FldLastTradeQuantity*FldPrice END) buy,
SUM(CASE FldBuySell WHEN 's' THEN FldLastTradeQuantity*FldPrice END) sell
FROM TblOrders
WHERE FldInstrumentID = instrument AND FldLastTradePrice != 0
AND FldLastTradeQuantity != 0
) a;
答案 1 :(得分:0)
试试这段代码 -
...
BEGIN
SELECT
SUM(IF(FldBuySell = 'b', FldLastTradeQuantity * FldPrice, 0)) AS total_buy,
SUM(IF(FldBuySell = 's', FldLastTradeQuantity * FldPrice, 0)) AS total_sell
INTO buy, sell
FROM
TblOrders
WHERE
FldInstrumentID = instrument
AND FldLastTradePrice != 0
AND FldLastTradeQuantity != 0;
SET profit = sell - buy;
END
...
修改强>
尝试在MySQL Command-Line Tool或其他工具中运行此代码模式并检查结果 -
SET @instrument = 10;
SET @buy = NULL;
SET @sell = NULL;
SET @profit = NULL;
CALL ProfitCalculation(@instrument, @buy, @sell, @profit);
SELECT @buy, @sell, @profit;
编辑2:
DELIMITER $$
CREATE PROCEDURE test(OUT buy FLOAT(10, 3), OUT sell FLOAT(10, 3), OUT profit FLOAT(10, 3))
BEGIN
SELECT 10, 15 INTO buy, sell; -- variant with SELECT INTO
SET profit = sell - buy;
END$$
DELIMITER ;
SET @buy = NULL;
SET @sell = NULL;
SET @profit = NULL;
CALL test(@buy, @sell, @profit);
SELECT @buy, @sell, @profit;
+------+-------+---------+
| @buy | @sell | @profit |
+------+-------+---------+
| 10 | 15 | 5 |
+------+-------+---------+
利润不是空。