SQL查询修改:插入临时表时trade_qty_in_lots为负对应trade_qty转负值的方法
你可以通过符号匹配逻辑调整trade_qty字段的取值,修改后的SQL语句如下:
SELECT bartt_code, bartt_code_description, mele_port_name, expiry_date, trade_price, trade_qty_in_lots, -- 同步trade_qty和trade_qty_in_lots的符号 SIGN(trade_qty_in_lots) * ABS(trade_qty) AS trade_qty, -- 计算字段同步使用处理后的trade_qty trade_price * (SIGN(trade_qty_in_lots) * ABS(trade_qty)) AS 'Price_x_Quantity' INTO #Table1 FROM kst_exchange_trade WHERE bartt_code IS NOT NULL GROUP BY bartt_code, bartt_code_description, mele_port_name, expiry_date, trade_price, trade_qty_in_lots, -- GROUP BY同步替换为处理后的trade_qty表达式 SIGN(trade_qty_in_lots) * ABS(trade_qty)
逻辑说明
SIGN(trade_qty_in_lots)会返回对应字段的符号属性:字段为正返回1、为负返回-1、为0返回0ABS(trade_qty)取trade_qty的绝对值,和上面的符号值相乘后,即可保证trade_qty和trade_qty_in_lots的符号完全一致
如果你的数据库不支持SIGN函数,可以改用CASE表达式实现相同逻辑,替换trade_qty部分的代码即可:
CASE WHEN trade_qty_in_lots < 0 THEN -ABS(trade_qty) ELSE ABS(trade_qty) END AS trade_qty
内容的提问来源于stack exchange,提问作者Davis
相关产品推荐
相关产品推荐

