You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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返回0
  • ABS(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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:48:00