Oracle SQL - 奇怪的“ORA-00907 缺少右括号”错误

Posted

技术标签:

【中文标题】Oracle SQL - 奇怪的“ORA-00907 缺少右括号”错误【英文标题】:Oracle SQL - Strange 'ORA-00907 Missing Right Parenthesis' error 【发布时间】:2009-08-07 05:01:11 【问题描述】:

我编写了一个查询来测试一个简单的线性回归,以获得两组体重测量值之间的最佳拟合线。它应该有望返回如下结果,但它会抛出一个奇怪的错误

'ORA-00907 缺少右括号'

并且 TOAD 指向它说的部分:

case ( when trn.wid_location = 28.3 then 

我一直在梳理它是否缺少括号,但我认为这不是问题,因为如果我将 case 语句替换为

100 as mine,

错误消失,查询执行。

有什么想法吗?

干杯,

汤米

select
      decode(wid_location,28.3,'CL',29.6,'DA') as site,
      (n*sum_xy - sum_x*sum_y)/(n*sum_x_sq - sum_x*sum_x) as m,
      (sum_y - ((n*sum_xy - sum_x*sum_y)/(n*sum_x_sq - sum_x*sum_x))*sum_x)/n as b 

from (
        select 
               wid_location,
               sum(wids) as sum_x,
               sum(mine) as sum_y,
               sum(wids*mine) as sum_xy,
               sum(wids*wids) as sum_x_sq,
               count(*) as n

        from (                                                                        
                select 
                       trn.wid_location,
                       con.empty_weight_total as wids,                                                                    
                       case ( 
                              when trn.wid_location = 28.3 then con.empty_weight_total*0.900-1.0
                              when trn.wid_location = 29.6 then con.empty_weight_total*0.950-1.5
                              end                
                            ) as mine   

                from widsys.train trn
                     inner join widsys.consist con
                     using (train_record_id)

                where mine_code = 'YA'
                      and to_char(trn.wid_date,'IYYY') = 2009
                      and to_char(trn.wid_date,'IW') = 29

                                 )

        group by wid_location
     )

这是我很高兴看到的结果

--    +----------+--------+----------+
--    | SITE     | M      | B        |
--    +----------+--------+----------+
--    | CL       | 0.900  | -1.0     |
--    +----------+--------+----------+
--    | DA       | 0.950  | -1.5     |
--    +----------+--------+----------+

【问题讨论】:

【参考方案1】:

T 认为 case 的语法不正确。

执行以下操作:

SELECT last_name, commission_pct,
  (CASE commission_pct 
    WHEN 0.1 THEN ‘Low’ 
    WHEN 0.15 THEN ‘Average’
    WHEN 0.2 THEN ‘High’ 
    ELSE ‘N/A’ 
  END ) Commission
FROM employees ORDER BY last_name; 

【讨论】:

【参考方案2】:

尝试去掉 case 语句中的两个括号。你不需要它们。

可以是:

case  when trn.wid_location = 28.3 then con.empty_weight_total*0.900-1.0
      when trn.wid_location = 29.6 then con.empty_weight_total*0.950-1.5 end as mine 

【讨论】:

以上是关于Oracle SQL - 奇怪的“ORA-00907 缺少右括号”错误的主要内容,如果未能解决你的问题,请参考以下文章

Oracle SQL - 奇怪的“ORA-00907 缺少右括号”错误

奇怪的 Oracle SQL“无效标识符”错误

一个奇怪的 Oracle SQL,它来自 3 个表,但在 where 子句中只留下了 join 2?

Oracle“插入”命令的奇怪行为

使用 EclipseLink 在 Oracle 上执行查询时出现奇怪的行为

oracle apex 奇怪的零除法问题