【问题标题】:sql oracle case 'from keyword not found where expected'sql oracle case '在预期的地方找不到关键字'
【发布时间】:2016-11-04 10:32:00
【问题描述】:

我正在尝试选择一个现有列并使用缩进的代码创建另一个列,但我得到了“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

【问题讨论】:

  • 请编辑您的问题并提供示例数据和所需结果。无论您如何尝试修复它,您的代码都有多个错误。导致此错误的具体问题是from 之前的逗号,但删除它确实不会修复查询。
  • 你需要做什么?
  • Oracle 没有BOOLEAN 数据类型。所以,除了所有的语法问题之外,在你修复它们之后,你会遇到这个问题,而且你将无法轻易修复它。您将需要确定 DesiredColumnName 是否是您所存储的内容的好列名?通过检查约束)只保存值01,或者是CHAR(1),只有值'T''F',或者可能是'Y''N'

标签: sql oracle


【解决方案1】:

我想这就是你要找的东西

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)

【讨论】:

  • 我也会投票支持的第二个脚本。最合乎逻辑的。
【解决方案2】:

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

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;

【讨论】:

    【解决方案3】:

    在 from 之前删除逗号 ','。

    选择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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多