2017-08-07 108 views
1

我有一個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 
+0

編輯你的問題,顯示的結果是什麼樣子。 –

回答

1

替換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 
-1

這是世界衛生大會你想要嗎?

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始終爲正。

0

試試這個:

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 
0
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 
    )