带有子查询的SQL Case语句

时间:2017-09-15 21:38:21

标签: sql-server tsql case-statement

有人可以告诉我我的个案陈述有什么问题吗?

select
(case 
       when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '17418')= 'C' Then 'Cancelled'<br/>
       when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '100020')= 'CL' Then 'Closed'<br/>
       when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '1105')= 'R' Then 'Reserved'<br/>
       when (select top 1 descr from t1 inner join t2 on t2.id = t1.id where t1.code = '1106')= 'S' Then 'Scheduled' <br/>
       else null end ) <br/>
From table

1 个答案:

答案 0 :(得分:1)

我想你想做这样的事情:)

select CASE t1.descr   
  WHEN 'C' THEN 'Cancelled' 
  WHEN 'CL' THEN 'Closed'  
  WHEN 'R' THEN 'Reserved' 
  WHEN 'S' THEN 'Schedule' 
  ELSE  
END as  descr_Text 
  from t1 
 inner join t2 on t2.id = t1.id
where t1.code in('17418','100020','1105',....)