【问题标题】:Oracle SQL query to fetch the values with the help of "Case When" ClauseOracle SQL 查询在“Case When”子句的帮助下获取值
【发布时间】:2015-04-07 16:01:57
【问题描述】:

我正在尝试在“CASE WHEN THEN”子句的帮助下执行 oracle sql 查询以获取一些值,但显示一些错误提示:

ORA-00913: 值太多 00913. 00000 - “值太多” *原因:
*行动: 行错误:13 列:5

【问题讨论】:

  • 您正在尝试做什么。在then 子句之后不应该有任何选择语句。应该只有标量值。
  • @Exhausted,感谢您的评论。好的,那么我应该为这个条件编写什么类型的查询?
  • @Exhausted, Condition like if () { 在某些条件下选择值} else { 在其他条件下选择值}

标签: oracle select oracle11g case sql-view


【解决方案1】:

你所做的类似于:

with d1 as (select 1 col1, 'A' col2 from dual union select 2, 'B' from dual),
d2 as (select 1 col1, 'Q' col2, 'W' col3, 'E' col4 from dual),
d3 as (select 2 col1, 'R' col2, 'T' col3, 'Y' col4 from dual)
select d1.col1, d1.col2, 
    case d1.col2 
    when 'A' then 
      (select d2.col2, d2.col3, d2.col4 from d2 where d2.col1 = d1.col1)
    else 
      (select d3.col2, d4.col3, d5.col4 from d2 where d2.col1 = d1.col1)
    end
  from d1

然后你得到 ORA-00913: too many values

你应该做的可能是这样的:

with d1 as (select 1 col1, 'A' col2 from dual union select 2, 'B' from dual),
d2 as (select 1 col1, 'Q' col2, 'W' col3, 'E' col4 from dual),
d3 as (select 2 col1, 'R' col2, 'T' col3, 'Y' col4 from dual)
select d1.col1, d1.col2 decision_field, 
    case d1.col2 when 'A' then d2.col2 else d3.col2 end col2,  
    case d1.col2 when 'A' then d2.col3 else d3.col3 end col3,  
    case d1.col2 when 'A' then d2.col4 else d3.col4 end col4  
  from d1
    left join d2 on d2.col1 = d1.col1
    left join d3 on d3.col1 = d1.col1

结果:

col1 decision_field col2 col3 col4
1    A              Q    W    E
2    B              R    T    Y

【讨论】:

  • 您的查询运行良好,但在我的情况下我仍然无法做到。我只想在其他条件下设置一些条件,例如: if (condition1 = 'y') { condition2 condition3 condition4 } else { condition5 condition6 }
  • 我认为可以通过重组joins或者使用union来实现。但是你没有清楚地说明你想要达到的目标。 table2 中有什么(我只看到查询中提到的字段 FUND_ORG_SECURITY_IND),table1 和 table2 之间的关系是什么。这真的是来自from table1, table2, table3 的carthesian 产品吗?您当前的内部查询(这些在then ... 和else ... 之后)对您有用吗?如果您附上所需输出的示例列表,那就太好了。如果没有这些提示,我将无法提供帮助。
猜你喜欢
  • 2016-02-23
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2017-05-30
  • 1970-01-01
相关资源
最近更新 更多