sql oracle case' from keyword not found of expected'

时间:2016-11-04 10:32:01

标签: sql oracle

我试图选择一个现有的列并使用缩进的代码创建另一个列,但我可以选择找不到预期的' ORA-00923:FROM关键字'错误或“缺少”关键字'错误。 (我是初学者)

你能帮帮我吗?

编辑:id是一个整数,description是一个字符串 我想为每个ID创建一个列,当描述值匹配' VALUE1'时,该列将用作标记。或者' VALUE2'。在表1中,只有从值的标识符到字符串的映射,在表2中我有所有记录。

select id as ID, 

    'cast( select description from db.table_1 
    join db.table_2 on table_2.id = table_1.id
    case when (table_1.description in ('VALUE1', 'VALUE2')) then '1'
    else '0' 
    end
    ) as boolean' as DesiredColumnName,

from db.table_2

3 个答案:

答案 0 :(得分:0)

我认为这就是你正在寻找的东西

select t2.id, decode(t1.description, 'VALUE1', 1, 'VALUE2', 1 ,0) as DesiredColumnName 
 from db.table2 t2 inner join db.table1 t1 on (t2.id = t1.id)

答案 1 :(得分:0)

如果我猜测你真正想做什么:

select t2.id,
       (case when exists (select 1
                          from table_1 t1
                          where t1.description in ('VALUE1', 'VALUE2') and
                                t2.id = t1.id   
                         )
             then 1 else 0
       end) as DesiredColumnName,
from db.table_2 t2;

答案 2 :(得分:0)

从之前删除逗号','。

选择ID作为ID,

'cast( select description from db.table_1 
join db.table_2 on table_2.id = table_1.id
case when (table_1.description in ('VALUE1', 'VALUE2')) then '1'
else '0' 
end
) as boolean' as DesiredColumnName

来自db.table_2