我有一個sql查詢,我想添加另一列,同時檢查case語句字段是否具有正值,以便將結果顯示爲Sales或Refund。例如,我要檢查,如果總計爲負或正值。如果是正面顯示其他銷售顯示退款查詢以下檢查一個Case語句字段是正數還是負數Sql
例子:
Select F01,
CASE
WHEN R.F1034=3 THEN convert(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END as Total
FROM RPT_CLT R
我有一個sql查詢,我想添加另一列,同時檢查case語句字段是否具有正值,以便將結果顯示爲Sales或Refund。例如,我要檢查,如果總計爲負或正值。如果是正面顯示其他銷售顯示退款查詢以下檢查一個Case語句字段是正數還是負數Sql
例子:
Select F01,
CASE
WHEN R.F1034=3 THEN convert(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END as Total
FROM RPT_CLT R
替換XXX
與你的領域/計算
SELECT CASE WHEN XXX >= 0 THEN 'Sales' ELSE 'Refund' END AS newfield
在您的例子:
SELECT F01,
CASE WHEN R.F1034 = 3
THEN CONVERT(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END AS Total,
CASE WHEN (CASE WHEN R.F1034 = 3
THEN CONVERT(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END) >= 0 THEN 'Sales' ELSE 'Refund' END AS newfield
FROM RPT_CLT R
這是世界衛生大會你想要嗎?
Select F01,
(CASE WHEN R.F1034 = 3
THEN convert(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END) as Total,
(CASE WHEN R.F1034 = 3 AND F65 > 0 THEN 'SALES' ELSE 'REFUND'
END) as which
FROM RPT_CLT R
,你可能想ABS()
它發生,我這樣Total
始終爲正。
試試這個:
Select F01,
CASE
WHEN R.F1034=3 THEN convert(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END as Total,
CASE
WHEN (CASE WHEN R.F1034=3
THEN convert(DOUBLE PRECISION, F65 * 100) * 10
ELSE 0
END)>=0
THEN 'Sales'
ELSE 'Refund'
END as Result
FROM RPT_CLT R
select F01,
Total,
case when total>=0 then 'Sales' else 'Refund' end as new
from (
select F01,
case when R.F1034 = 3
then convert(double precision, F65*100)*10
else 0
end as Total
from RPT_CLT
)
編輯你的問題,顯示的結果是什麼樣子。 –