2014-03-04 100 views
3

我有一個表名對具有和或條件的表格的陳述陳述?

Spend: 

id value type date   total 
1 21  ar 3/3/2012  45 
2 24  az 3/3/2012  55 
3 25  az 3/3/2012  55 
4 25  az 3/3/2012  45 
5 25  ar 3/3/2012  45 

條件: APR =(值= 25,然後鍵入像 'AR'),或在SELECT語句(總共55)

我怎麼能寫case語句在單選語句中使用#temp和cte。

SELECT語句的輸出必須

id value type date   total APR 
1 21  ar 3/3/2012  45 0 
2 24  az 3/3/2012  55 1 
3 25  az 3/3/2012  55 1 
4 25  az 3/3/2012  45 0 
5 25  ar 3/3/2012  45 1 

回答

1
SELECT IIF((Value = 25 and type like 'ar') or (total=55)', 1, 0) AS APR, * 
FROM Spend 
3

試試這個

SELECT id,value,type,date,total,APR, 
     CASE WHEN (value=25 and type like 'ar') OR (total=55) Then 1 ELSE 0 END AS [Apr] 
FROM Table1 

CASE