2014-12-03 77 views
1

列表我需要寫一個條件,以確保病情不等於SQL語句的情況下,比較不等於與條件

select * from xtable x 
inner join ytable y 
on x.column1 =y.column1 
inner join ztable z 
on y.column2 =z.column2 
where x.column4 = 'abc' 
and y.column3 <> cast(z.column4 as bit) 
and y.column4 = 'xyz' 

我得到錯誤的造型,因爲y.column3只有0或1個值,其中z.column4具有多於0和1的值我怎樣才能使其他值爲0或1取決於值。

我sepreatly寫的select語句打印0和1,取決於病症

select case z.column4 
when 'True' then 1 
when 'false' then 0 
when 'From: False To True' then 1 
when 'From: True to False' then 0 
end as status 
from ztable z 

我不能,如果寫這樣一起incorproate兩個,其給予了錯誤下面

select * from xtable x 
    inner join ytable y 
    on x.column1 =y.column1 
    inner join ztable z 
    on y.column2 =z.column2 
    where x.column4 = 'abc' 
    and y.column3 <> (case when z.column4 = 'True' then 1 
    when z.column4 = 'false' then 0 
    when z.column4 = 'From: False To True' then 1 
    when z.column4 = 'From: True to False' then 0) 
    and y.column4 = 'xyz' 

回答

2

你幾乎在那裏,但是在最後一個查詢中缺少ENDCASE

..... 
AND y.column3 <> CASE z.column4 
         WHEN 'True' THEN 1 
         WHEN 'false' THEN 0 
         WHEN 'From: False To True' THEN 1 
         WHEN 'From: True to False' THEN 0 
        END  
    .....