带有和/或条件的表的案例陈述?

时间:2014-03-04 03:19:56

标签: sql-server sql-server-2008 tsql sql-server-2012 case

我有一个表名

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')或(总计= 55)

如何在select语句中编写case语句而不在单选select语句中使用#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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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