我正在使用以下代碼將資料插入到 SQL 中的臨時表中:
SELECT bartt_code, bartt_code_description, mele_port_name, expiry_date, trade_price, trade_qty_in_lots, trade_qty, (trade_price*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, trade_qty
我遇到了一個問題,當 trade_qty_in_lots 為負時,trade_qty 應該為負。我如何將其構建到我的查詢中以進行此更改?

uj5u.com熱心網友回復:
該sign()函式將回傳一個可用于匹配負數/正數的值。
sign(trade_qty_in_lots) * abs(trade_qty)
如果您可以保證trade_qty始終為正值,那么絕對值自然是不必要的并且可以洗掉。
uj5u.com熱心網友回復:
如果只想對齊符號,可以隨時將trade_qty欄位轉換為運算式,例如:
IIF (
trade_qty_in_lots >= 0,
ABS(trade_qty),
-1 * ABS(trade_qty)
) AS trade_qty
請注意,我添加了一個ABS以涵蓋所有四種情況:
- 兩個跡象都是積極的
- 兩個符號都為負(未顯示在問題中)
trade_qty是正的,trade_qty_in_lots是負的trade_qty為負,trade_qty_in_lots為正(未顯示在問題中)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/328801.html
標籤:sql sql-server
上一篇:日期范圍內的SQL聯接事務
下一篇:SQLServer常規透視
