2017-09-15 171 views
-1

有人能告訴我我的case語句有什麼問題嗎?帶有子查詢的SQL Case語句

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

這是有效的代碼還是僞代碼?什麼是「從表格」? – Parfait

+0

它只是拉第一個陳述而不是其他 – Jason312

回答

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',....)